首页 Python通用模块视频教程 Python re模块compile()方法使用详解
pay pay

Python re模块compile()方法使用详解

日期: 五月 4, 2023, 10:23 a.m.
阅读: 139
作者: Python自学网-村长

摘要: Python re模块compile()方法使用详解

import re

# compile(pattern, flags=0)编译,一般用于同一个正则被调用多次
content = '''<title>this is python</title>'''
res = re.compile('<title>([\w\W]*)</title>')
res.match(content)
# 后面的方法都是re模块提供的,但是使用compile也可以使用,只要注意传参就可以了
# flages参数值:
re.compile('', flags=re.I)  # 大小写不敏感
res = re.compile('aa', flags=re.I)
print(res.match('Aa'))
re.compile('', flags=re.S)  # 点的任意匹配模式,改变'.'的行为
re.compile('', flags=re.L)  # 使预定字符类 \w \W \b \B \s \S 取决于当前区域设定
re.compile('', flags=re.M)  # 多行模式,改变'^'和'$'的行为
re.compile('', flags=re.X)  # 详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释
re.compile('', flags=re.U)  # 使预定字符类 \w \W \b \B \s \S \d \D 取决于unicode定义的字符属性

 

原创视频,版权所有,未经允许,切勿转载,违者必究!
回顶部