python解析之正则表达式,由浅入深带你轻松学会正则表达式(07匹配模式)

简介:所谓匹配模式就是对匹配的原则进行整体的修饰。

示例代码:

import re

# 忽略大小写re.I
s = re.search(r'hello', 'HELLO world', re.I)
print(s.group())

# 正则默认是单行匹配,使用 re.M 可以进行多行匹配
s = re.search(r'^hello', 'sdhasdh\nhello world', re.M)
print(s.group())

# 使.匹配任意字符,作为单行处理,忽略\n
# string = '<div>hello</div>'
string = '''<div>
hello
</div>'''
s = re.search(r'<div>.*?</div>', string, re.S)

if s:
    print(s.group())

运行结果:

猜你喜欢

转载自blog.csdn.net/z_xiaochuan/article/details/81586677