1.
re.sub? Signature:re.sub (Pattern, REPL, String, Count=0, flags=0) Docstring:return the string obtained by replacing the L Eftmostnon in string by Thereplacement repl. or a callable; if in it is processed. is a callable, it ' s passed the match object and must returna replacement string to be used.
Parameter description: Pattern string, can be numerically named can also name name (\g<1>==\1) (?). P<name>----------------\g<name>)
Repl replaced string can also be a function string source string
Count substitution number of times
The value of flag is:
Re. I make the match to case insensitive re. L do localization recognition (locale-aware) to match re. M multi-line match, affecting ^ and $re. S makes. Matches all characters including line breaks RE.U characters based on the Unicode character set. This flag affects \w, \w, \b, and \bre.x this flag by giving you a more flexible format so that you can write regular expressions easier to understand
2. Example
def Replace_digit (m): = u' 0 123456789 ' = int (m.group ()) return = u'March 27, 1990 '= re.sub (u'\d' , Replace_digit, S, count=4)print# March 27, 1990
' 2017-01-22 ' = re.sub ('(\d{4})-(\d{2})-(\d{2})', R'\2-\3-\1' , s) Print # 01-22-2017
Python re.sub