常用的匹配符
贪婪模式
reg = 'He.(\d).Demo$'
非贪婪模式
reg = 'He.?(\d).Demo$'
常规匹配
泛匹配 (使用* ?)
匹配模式
re.S : 忽略换行符
re.A : 对ASCII 码有用,对Unicode忽略
re.DEBUG : 显示调试信息
re.I : 忽略字母大小写
re.X :
常用方法官方参考
- re.search(pattern,string,flags = 0 ) : 匹配方法
- re.findall(pattern,string,flags = 0 ): 匹配返回列表
- re.match(pattern,string,flags = 0 ) : 匹配返回匹配对象
- re.sub() : 正则表达式替换
- re.fullmatch(pattern,string,flags = 0 ): 返回完全匹配
- re.split(pattern,string,maxsplit = 0,flags = 0 ): 匹配拆分
- re.finditer(pattern,string,flags = 0 ): 匹配返回一个迭代器
- re.subn(pattern,repl,string,count = 0,flags = 0 ): 返回元组
- re.escape(pattern) : 字符转义
-
print(re.escape('python.exe'))
-
- Match.groups(default=None) : 获取所有匹配的组
Match.groupdict(default=None) 匹配字典化
m = re.match(r"(?P\w+) (?P\w+)", "Malcolm Reynolds")
Match.groupdict(default=None)
{'first_name': 'Malcolm', 'last_name': 'Reynolds'}Match.start([group]), Match.end([group]) 匹配开始结尾位置