re.I IGNORECASE, 忽略大小写的匹配模式
-
In [59]: s = 'hello World!'
-
In [60]: regex = re.compile("hello world!", re.I)
-
In [61]: regex.match(s).group()
-
Out[61]: 'hello World!'
re.M MULTILINE,多行模式, 改变 ^ 和 $ 的行为
-
In [63]: s
-
Out[63]: 'first line\nsecond line\nthird line'
-
In [64]: pattern=re.compile(r'^\w+')
-
In [65]: re.findall(pattern,s)
-
Out[65]: ['first']
-
In [67]: pattern=re.compile(r'^\w+',re.M)
-
In [68]: re.findall(pattern,s)
-
Out[68]: ['first', 'second', 'third']
re.S DOTALL,此模式下 '.' 的匹配不受限制,可匹配任何字符,包括换行符,也就是默认是不能匹配换行符
-
In [62]: s = '''first line
-
...: second line
-
...: third line'''
-
In [71]: regex=re.compile('.+',re.S)
-
In [73]: regex.findall(s)
-
Out[73]: ['first line\nsecond line\nthird line']
-
In [74]: regex=re.compile('.+')
-
In [75]: regex.findall(s)
-
Out[75]: ['first line', 'second line', 'third line']
re.X VERBOSE,冗余模式, 此模式忽略正则表达式中的空白和#号的注释
-
email_regex = re.compile("[\w+\.]+@[a-zA-Z\d]+\.(com|cn)")
-
email_regex = re.compile("""[\w+\.]+ # 匹配@符前的部分
-
@ # @符
-
[a-zA-Z\d]+ # 邮箱类别
-
\.(com|cn) # 邮箱后缀 """, re.X)