正则表达式标记属于正则的扩展表示法,格式:(?iLmsx)
各个字符的含义是:
i:不区分大小写的匹配
L:根据所使用的本地语言环境通过\w、\W、\b、\B、\s、\S实现匹配
s:使点号(.)可以匹配\n
m:能够在目标字符串实现跨行都说,而不必将整个字符串视为单个实体
x:该标记允许用户通过抑制在正则表达式中使用空白符(除了在字符类中或者发斜线转义中)来创建更易读的正则表达式
举例:
- 参数i
>>> re.findall('(?i)yes','yes,Yes,YES!!')
['yes', 'Yes', 'YES']
>>>
- 参数m
>>> re.findall('(?m)(^th[\w ]+)',""")
This line is the first,
another line,
that line,it's the best
""")
['This line is the first','that line']
- 参数s
>>> re.findall('(?s)th.+','''
The first line
the second line
the third line
''')
['the second line\nthe third line\n ']
>>> re.findall('th.+','''
The first line
the second line
the third line
''')
['the second line', 'the third line']