标签:pattern 正则 字符串 name def locale python re most return
1.
re.sub? Signature: re.sub(pattern, repl, string, count=0, flags=0) Docstring: Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it‘s passed the match object and must return a replacement string to be used.
参数说明:pattern模式字符串,可以数字命名也可以name命名(\g<1>==\1)(?P<name>----------------\g<name>)
repl 替换的字符串也可以是函数 string源串
count替换的次数
flag的值为:
re.I 使匹配对大小写不敏感 re.L 做本地化识别(locale-aware)匹配 re.M 多行匹配,影响^和$ re.S 使.匹配包括换行在内的所有字符 re.U 根据Unicode字符集解析字符。这个标志影响\w、\W、 \b和\B re.X 该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解
2.实例
def replace_digit(m): ss = u‘〇一二三四五六七八九‘ index = int(m.group()) return ss[index] s = u‘1990年3月27日‘ result = re.sub(u‘\d‘, replace_digit, s, count=4) print(result) # 一九九〇年3月27日
s = ‘2017-01-22‘ s = re.sub(‘(\d{4})-(\d{2})-(\d{2})‘, r‘\2-\3-\1‘, s) print(s) # 01-22-2017
标签:pattern 正则 字符串 name def locale python re most return
原文地址:https://www.cnblogs.com/yitiaodahe/p/9217176.html