python 正则表达式

来源:互联网 发布:swift 源码下载 编辑:程序博客网 时间:2024/05/21 21:47

re.I:不区分大小写

#!/usr/bin/env pythonimport res = re.findall(r'error', 'error\nError\nERROR')print ss = re.findall(r'error', 'error\nError\nERROR', re.I)print s

结果:

['error']['error', 'Error', 'ERROR']




re.S:使 "." 特殊字符完全匹配任何字符,包括换行;没有这个标志, "." 匹配除了换行外的任何字符。

#!/usr/bin/env pythonimport res = re.findall(r"a(\d+)b.*a(\d+)b", "a12b\na23b")print ss = re.findall(r"a(\d+)b.*a(\d+)b", "a12b\na23b", re.S)print s
结果:
[][('12', '23')]

re.M:^$标志将会匹配每一行,默认^和$只会匹配第一行

#!/usr/bin/env pythonimport res = re.findall(r"^a(\d+)b", "a23b\na34b")print ss = re.findall(r"^a(\d+)b", "a23b\na34b", re.M)print s
结果:
['23']['23', '34']



0 0
原创粉丝点击