对字符串中的文本做查找和替换
1、对于简单的文本模式,使用 str.replace() 即可。
In [1]: text = 'yeah, but no, but yeah, but no, but yeah'
In [2]: text.replace('yeah', 'yep')
Out[2]: 'yep, but no, but yep, but no, but yep'
2、对于复杂的模式,使用 re 模块中的 sub() 函数。
In [3]: text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
In [4]: import re
In [5]: re.sub(r'(\d+)/(\d+)/(\d+)',r'\3-\1-\2',text)
Out[5]: 'Today is 2012-11-27. PyCon starts 2013-3-13.'
sub() 中的第一个参数是要匹配的模式,第2个参数是要替换上的模式。
类似“\3” 这样的是代表着模式中捕获组的数量。
如果打算执行相同的重复替换,可以先将模式编译以获得更好的性能。
In [7]: pattern = re.compile(r'(\d+)/(\d+)/(\d+)')
In [8]: pattern.sub(r'\3-\1-\2',text)
Out[8]: 'Today is 2012-11-27. PyCon starts 2013-3-13.'
3、对于更复杂的替换,可以传递一个回调函数来替代
In [11]: from calendar import month_abbr
In [12]: def change_date(m):
...: mon_name = month_abbr[int(m.group(1))]
...: return '{} {} {}'.format(m.group(2), mon_name, m.group(3))
...:
In [13]: pattern.sub(change_date, text)
Out[13]: 'Today is 27 Nov 2012. PyCon starts 13 Mar 2013.'
calendar.month_abbr
一个数组,表示当前语言环境中一年的缩写月份。 这符合1月份的正常惯例为1号,所以它的长度为13,而month_abbr [0]
是空字符串。
替换回调函数的输入参数是一个匹配对象,由match()
或 find()
返回。 用 .group()
方法来提取匹配中的特定的部分。
如果还想知道替换的过程中,一共完成了多少次替换,可以使用 re.subn()
。
re.subn(pattern, repl, string, count=0, flags=0)
执行与sub()
相同的操作,但返回一个元组(new_string,number_of_subs_made)
。
In [14]: nexttext, n = pattern.subn(change_date, text)
In [15]: nexttext
Out[15]: 'Today is 27 Nov 2012. PyCon starts 13 Mar 2013.'
In [16]: n
Out[16]: 2