问题:对字符串中的文本做查找和替换

解决方案:

1、对于简单模式:str.replace(old, new[, max])

2、复杂模式:使用re模块中的re.sub(匹配的模式, newstring, oldstring[,替换个数])函数

3、re.subn()可以获得替换的总次数

# example.py
#
# Examples of simple regular expression substitution import re #simple sample
text1='yeah,but no,but yeah,but no,but yeah,but no,but yeah'
print (text1.replace('yeah','yeh'))
print (text1.replace('no','yes',2))
print ('---------------------------')
# Some sample text
text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
# (a) Simple substitution \3-表示匹配的模式中第3个模式组
print(datepat.sub(r'\3-\1-\2', text)) #等价于print (re.sub(r'(\d+)/(\d+)/(\d+)',r'\3-\1-\2', text))
print ('*****************************')
# (b) Replacement function 替换回调函数
from calendar import month_abbr
def change_date(m):
mon_name = month_abbr[int(m.group(1))]
return '{} {} {}'.format(m.group(2), mon_name, m.group(3)) print(datepat.sub(change_date, text))
print (re.sub(r'(\d+)/(\d+)/(\d+)',change_date, text))
print ('++++++++++++++++++++++++++++++++')
# 通过re.subn()获取替换的总次数
newtext,n=datepat.subn(r'\3-\1-\2', text) 
print (newtext)
print (n)
>>> ================================ RESTART ================================
>>>
yeh,but no,but yeh,but no,but yeh,but no,but yeh
yeah,but yes,but yeah,but yes,but yeah,but no,but yeah
---------------------------
Today is 2012-11-27. PyCon starts 2013-3-13.
*****************************
Today is 27 Nov 2012. PyCon starts 13 Mar 2013.
Today is 27 Nov 2012. PyCon starts 13 Mar 2013.
++++++++++++++++++++++++++++++++
Today is 2012-11-27. PyCon starts 2013-3-13.
2
>>>

【python cookbook】【字符串与文本】5.查找和替换文本的更多相关文章

  1. python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  2. 【python cookbook】【字符串与文本】6.以不区分大小写的方式对文本做查找和替换

    问题:以不区分大小写的方式对文本做查找和替换 解决方法:使用re模块,并对各种操作都添加上re.IGNORECASE标记 text='UPPER PYTHON,lower python,Mixed P ...

  3. bat批处理 查找替换:批处理如何查找并替换文本里特定字符串中的部分内容

    批处理如何查找并替换文本里特定字符串中的部分内容 摘自:http://www.bathome.net/thread-43349-1-1.html 脚本如下: @if()==() echo off &a ...

  4. C# 在word中查找及替换文本

    C# 在word中查找及替换文本 在处理word文档时,很多人都会用到查找和替换功能.尤其是在处理庞大的word文档的时候,Microsoft word的查找替换功能就变得尤为重要,它不仅能让我们轻易 ...

  5. JS实现文本中查找并替换字符

    JS实现文本中查找并替换字符 效果图: 代码如下,复制即可使用: <!DOCTYPE html><html> <head> <style type=" ...

  6. Linux中在vim/vi模式下对文本的查找和替换

    查找: 1.vim  filename  进入一般模式下 2.查找和替换方法 /word    向下查找word 的字符串  例如  /chengtingting   向下查找字符chengtingt ...

  7. python cookbook第三版学习笔记四:文本以及字符串令牌解析

    文本处理: 假设你存在一个目录,下面存在各种形式的文件,有txt,csv等等.如果你只想找到其中一种或多种格式的文件并打开该如何办呢.首先肯定是要找到满足条件的文件,然后进行路径合并在一一打开. pa ...

  8. python cookbook 字符串和文本

    使用多个界定符分隔字符串 import re line = 'asdf fjdk; afed, fjek,asdf, foo' print(re.split(r'[;,\s]\s*', line)) ...

  9. shell 字符串处理汇总(查找,替换等等)

    字符串: 简称“串”.有限字符的序列.数据元素为字符的线性表,是一种数据的逻辑结构.在计算机中可有不同的存储结构.在串上可进行求子串.插入字符.删除字符.置换字符等运算. 字符: 计算机程序设计及操作 ...

随机推荐

  1. 搭建和使用Docker私有仓库

    需要注意的是,从Docker Pool下载的镜像文件,与官方镜像文件是完全一致的. 安装Docker之后,可以是使用官方提供的registry镜像来搭建一套本地私有仓库环境:  docker run  ...

  2. sdut2169:Sequence(dp)

    题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2169 #include <iost ...

  3. 开发者必知的8款App快速开发工具

    开发者必知的8款App快速开发工具 “我有一个好创意,就差一个CTO……” “原生APP开发难度大,周期长,成本高,还没上线市场已经被占领了.” “APP版本迭代更新,都是企业的一道难关,没有一个一劳 ...

  4. 智能硬件+App移动新生态【北京、广州、深圳】

    智能硬件+App移动新生态[10.24北京站] 时间:2015年10月24日13:30-16:30 地点:Wepac空间(海淀区北四环西路68号左岸工社6层) 主办:APICloud.机智云.智石科技 ...

  5. checkbox的完美用户体验(转)

    如需查看效果-->自行建个html文件,或者-->原文:http://bbs.blueidea.com/thread-2711834-1-7.html 最常见的checkbox的使用: & ...

  6. UIPikerView的属性

    1.    numberOfComponents:返回UIPickerView当前的列数 NSInteger num = _pickerView.numberOfComponents; NSLog( ...

  7. C++Primer 第十二章

    //1.标准库提供了两种智能指针类型来管理动态对象,均定义在头文件memory中,声明在std命名空间. // shared_ptr:允许多个指针指向同一个对象. // unique_ptr:独占所指 ...

  8. Leetcode: Elimination Game

    There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...

  9. Applescript 带参数调用某个App的方法

    do shell script "open '/Users/eran/Documents/Workground/DragonAdventure/FlashCode/tools/SWFInfo ...

  10. 经常遇到Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be e

     遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...