Other common uses are to find all pattern-matched strings and replace them with different strings. The sub () method provides a replacement value, which can be a string or a function, and a string to be processed.
sub(replacement, string[, count = 0])
The returned string is replaced by a match that is not repeated on the leftmost side of the re in the string. If the pattern is not found, the character will be returned unchanged.
The optional parameter count is the maximum number of times a pattern match is replaced, and count must be a non-negative integer. The default value is 0 to replace all matches.
Here is a simple example of using the sub () method. It replaces the color name with the word "colour".
#!python>>> p = re.compile( ‘(blue|white|red)‘)>>> p.sub( ‘colour‘, ‘blue socks and red shoes‘)‘colour socks and colour shoes‘>>> p.sub( ‘colour‘, ‘blue socks and red shoes‘, count=1)‘colour socks and red shoes‘
The Subn () method acts like this, but returns a two-tuple that contains the new string and the number of substitution executions.
#!python>>> p = re.compile( ‘(blue|white|red)‘)>>> p.subn( ‘colour‘, ‘blue socks and red shoes‘)(‘colour socks and colour shoes‘, 2)>>> p.subn( ‘colour‘, ‘no colours at all‘)(‘no colours at all‘, 0)
Empty matches are replaced only if they are not next to the previous match.
#!python>>> p = re.compile(‘x*‘)>>> p.sub(‘-‘, ‘abxd‘)‘-a-b-d-‘
If you replace a string, any backslash in it will be processed. "\ n" will be converted to a newline character, "\ R" into a carriage return, and so on. An unknown escape such as "\j" remains intact. A reverse reference, such as "\6", is replaced by a quilt string that matches the corresponding group in the RE. This allows you to insert part of the original text in the replaced string.
This example matches the word "section" enclosed by "{" and "}" and replaces "section" with "subsection".
#!python>>> p = re.compile(‘section{ ( [^}]* ) }‘, re.VERBOSE)>>> p.sub(r‘subsection{\1}‘,‘section{First} section{second}‘)‘subsection{First} subsection{second}‘
You can also specify the use (? P<name>, ...) A named group of syntax definitions. "\g<name>" is matched by a substring with the group name "name", and "\g<number>" uses the corresponding group number. So "\g<2>" equals "\2", but can be ambiguous in the replacement string, such as "\g<2>0". ("\20" is interpreted as a reference to group 20 instead of a reference to group 2 followed by a letter "0".) )
#!python>>> p = re.compile(‘section{ (?P<name> [^}]* ) }‘, re.VERBOSE)>>> p.sub(r‘subsection{\1}‘,‘section{First}‘)‘subsection{First}‘>>> p.sub(r‘subsection{\g<1>}‘,‘section{First}‘)‘subsection{First}‘>>> p.sub(r‘subsection{\g<name>}‘,‘section{First}‘)‘subsection{First}‘
Substitution can also be a function that gives you even more control. If the substitution is a function, the function will be called by every non-repeating match in the pattern. At each invocation, the function is passed in MatchObject
as an argument to the object, so it can be used to calculate the replacement string and return it.
In the following example, the substitution function translates the decimal into 16 binary:
#!python>>> def hexrepl( match ):... "Return the hex string for a decimal number"... value = int( match.group() )... return hex(value)...>>> p = re.compile(r‘\d+‘)>>> p.sub(hexrepl, ‘Call 65490 for printing, 49152 for user code.‘)‘Call 0xffd2 for printing, 0xc000 for user code.‘
When a module-level re.sub () function is used, the pattern is used as the first parameter. The pattern may be a string or one RegexObject
; If you need to specify a regular expression flag, you must either use RegexObject
the first argument or use the pattern inline modifier, such as Sub ("(? i) B +", "X", "BBBB bbbb") returns ' X x '.
Search and replace