python正则表达式的使用
16lz
2021-01-22
常用的匹配符
贪婪模式
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]) 匹配开始结尾位置
正则表达式在线测试:https://regex101.com
更多相关文章
- 从字典中创建NumPy数组的最佳方法是什么?
- 分析标签集的最佳方法是什么?
- 在java中调用python方法
- Python中字典合并的实现方法
- Python 学习笔记【list的操作方法】
- 如何在/account / url模式之外使用Django-AllAuth注册表单?
- 使用pip安装tensorflow 0.80,python 使用tensorflow 0.80遇到的问
- 在save方法中创建两个对象
- 基于Python的XSS测试工具XSStrike使用方法