多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
| 字符 | 功能 | | --- | --- | |` |` | 匹配左右任意一个表达式 | | `(ab)` | 将括号中字符作为一个分组 | | `\num` | 引用分组num匹配到的字符串 | | `(?p<name>)` | 分组起别名 | | `(?P=name)` | 引用别名为name分组匹配到的字符串 | ~~~ # 匹配0-100的数字 # 0 10 11 12 13 20 30 40 100 # 1 re.match(r"[1-9]\d?$|0$|100$", "200") # 2 精简 re.match(r"[1-9]?\d?$|100$", "0") ~~~ ~~~ # 匹配路径 # file:///C:/Users/Administrator/Desktop//courseware/section.4.html re.match(r"", "") ~~~ ~~~ # 提取匹配内容 group,groups >>> result = re.match(r"(<h1>).*(</h1>)", "<h1>匹配分组</h1>") >>> result.group() '<h1>匹配分组</h1>' >>> result.group(1) '<h1>' >>> result.group(2) '</h1>' >>> result.groups() ('<h1>', '</h1>') >>> result.groups()[0] '<h1>' >>> result.groups()[1] '</h1>' ~~~ ~~~ # 分组引用匹配 >>> s = "<html><h1>haibo</h1></html>" >>> result = re.match(r"<(.+)><(.+)>.+</\2></\1>", s) >>> result.group() '<html><h1>haibo</h1></html>' >>> result.groups(1) ('html', 'h1') >>> result.groups(2) ('html', 'h1') ~~~ ~~~ # 分组别名匹配 >>> s = "<html><h1>haibo</h1></html>" >>> re.match(r"<(?P<haibo1>.+)><(?P<haibo2>.+)>.+</(?P=haibo2)></(?P=haibo1)>", s) <_sre.SRE_Match object; span=(0, 27), match='<html><h1>haibo</h1></html>'> ~~~