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

解决方案:

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. logback详细配置(三)

    转自:http://blog.csdn.net/haidage/article/details/6794540 <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NE ...

  2. https://my.oschina.net/reesechou/blog/492265

    https://my.oschina.net/reesechou/blog/492265

  3. LeetCode Basic Calculator

    原题链接在这里:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a s ...

  4. div元素呈圆环排列

    <style> .path { width: 300px; height: 300px; padding: 20px; border-radius: 50%; background: rg ...

  5. Android仿QQ窗口的抖动的动画效果

    就是仿照QQ窗口的抖动效果,在项目的res下创建anim文件夹,再创建两个xml文件:cycle.xml  . myanim.xml   cycle.xml  :   <?xml version ...

  6. Android -- Looper.prepare()和Looper.loop() —深入版

    Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理.handler其实可以看做是一个工具类,用来向消息队列中插入消息的. (1) Loope ...

  7. 删除SSMS中保存的帐户信息

    通常我们在对象资源管理器中连接服务器时,会发现在服务器名称下保存有之前的实例信息.随着连接增多,要找某个连接还得费劲.sql2012:此时可以删除C:\Users\Administrator\AppD ...

  8. debug 使用lldb

    http://www.zddhub.com/memo/2015/12/20/lldb-golang-debug/ go build -gcflags "-N -l" -o test ...

  9. 在Entity Framework 4.0中使用 Repository 和 Unit of Work 模式

    [原文地址]Using Repository and Unit of Work patterns with Entity Framework 4.0 [原文发表日期] 16 June 09 04:08 ...

  10. struts标签小记

    1.<s:iterator>标签的  奇偶数行使用不同样式 <s:iterator id="list" value="#request.listq&qu ...