String search and replacement
problem
You want to search and match the specified text pattern in the string
solution
For simple literal patterns , Use it directly str.repalce() The method can , such as :
>>> text = 'yeah, but no, but yeah, but no, but yeah'
>>> text.replace('yeah', 'yep')
'yep, but no, but yep, but no, but yep'
>>>
For complex patterns , Please use re Module sub() function . To illustrate this , Suppose you want to take the form 11/27/2012 The date string of is changed to 2012-11-27 . Examples are as follows :
>>> text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
>>> import re
>>> re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text)
'Today is 2012-11-27. PyCon starts 2013-3-13.'
>>>
sub() The first parameter in the function is the matched pattern , The second parameter is replace mode . Backslash numbers like n3 Point to the capture group number of the previous pattern . If you plan to do multiple substitutions with the same pattern , Consider compiling it first to improve performance . such as :
>>> import re
>>> datepat = re.compile(r'(\d+)/(\d+)/(\d+)')
>>> datepat.sub(r'\3-\1-\2', text)
'Today is 2012-11-27. PyCon starts 2013-3-13.'
>>>
For more complex substitutions , You can pass a replacement callback function instead of , such as :
>>> 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))
...
>>> datepat.sub(change_date, text)
'Today is 27 Nov 2012. PyCon starts 13 Mar 2013.'
>>>
The parameter of a replacement callback function is a match object , That is to say match() perhaps find() Returned object . Use group() Method to extract specific matching parts . The callback function finally returns the replacement string .
If in addition to the result of replacement , You also want to know how many substitutions have taken place , have access to re.subn() Instead of . such as :
>>> newtext, n = datepat.subn(r'\3-\1-\2', text)
>>> newtext
'Today is 2012-11-27. PyCon starts 2013-3-13.'
>>> n
2
>>>
Discuss
About regular expression search and replacement , Shown above sub() The method has basically covered all . Actually, the hardest part is writing regular expression patterns ,