Python学习手册之捕获组和特殊匹配字符串
在上一篇文章中,我们介绍了 Python 的字符类和对元字符进行了深入讲解,现在我们介绍 Python 的捕获组和特殊匹配字符串。查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/10036661.html
捕获组
可以通过用括号包围正则表达式的部分来创建组,意味着一个组可以作为元字符 (例如 * 和 ?) 的参数。
import re pattern = r"python(ice)*"
string1 = "python!"
string2 = "ice"
string3 = "pythonice" match1 = re.match(pattern,string1)
match2 = re.match(pattern,string2)
match3 = re.match(pattern,string3) if match1:
print(match1.group())
print("match 1") if match2:
print(match2.group())
print("match 2") if match3:
print(match3.group())
print("match 3")
运行结果:
>>>
python
match 1
pythonice
match 3
>>>
上面的例子 (ice) 表示捕获组。
之前介绍元字符和字符类时,我们都用到了 group 函数访问捕获组中的内容。group(0) 或 group() 返回全部匹配,group(n) 调用 n 大于 0 返回第 n 组匹配。groups() 返回一个包含所有捕获组的元组。
import re pattern = r"j(av)(ap)(yt(h)o)n"
string = "javapythonhtmlmysql" match = re.match(pattern,string) if match:
print(match.group())
print(match.group(0))
print(match.group(1))
print(match.group(2))
print(match.groups())
运行结果:
>>>
javapython
javapython
av
ap
('av', 'ap', 'ytho', 'h')
>>>
捕获组同时可以嵌套,也就是说一个组可以是另一个组的子集。
有一些特殊的捕获组,它们叫非捕获组和命名捕获组。
命名捕获组的格式是 (?p<name>...),其中 name 是组的名称,...是要匹配的表达式。它们的行为与正常组完全相同,除了可以通过索引访问还可以通过 group(name) 方式访问它们。
非捕获组的格式是 (?:...)。非捕获组值匹配结果,但不捕获结果,也不会分配组号,当然也不能在表达式和程序中做进一步处理。
import re pattern = r"(?P<python>123)(?:456)(789)"
string = "" match = re.match(pattern,string) if match:
print(match.group("python"))
print(match.groups())
运行结果:
>>>
123
('123', '789')
>>>
或匹配的元字符 |,red|blue 表示匹配 red 或者 blue。
import re string1 = "python"
string2 = "pyihon"
string3 = "pylhon"
pattern = r"py(t|i)hon" match1 = re.match(pattern,string1)
match2 = re.match(pattern,string2)
match3 = re.match(pattern,string3) if match1:
print(match1.group())
print("match 1") if match2:
print(match2.group())
print("match 2") if match3:
print(match3.group())
print("match 3")
运行结果:
>>>
python
match 1
pyihon
match 2
>>>
特殊匹配字符串
特殊序列
在正则表达式中可以使用各种的捕获组序列。它们被写成反斜杠,后面跟着另一个数字字符。
特殊序列是一个反斜杠和一个介于 1 到 99 之间的数字,比如:\1。数字自发表示捕获组的序列,也就是说我们可以在正则表达式里引用先前的捕获组。
import re string1 = "html python"
string2 = "python python"
string3 = "java java"
pattern = r"(.+) \1" match1 = re.match(pattern,string1)
match2 = re.match(pattern,string2)
match3 = re.match(pattern,string3) if match1:
print(match1.group())
print("match 1") if match2:
print(match2.group())
print("match 2") if match3:
print(match3.group())
print("match 3")
运行结果:
>>>
python python
match 2
java java
match 3
>>>
注意:(.+) \1 不等同于 (.+)(.+),因为 \1 引用第一组的表达式,即匹配表达式本身,而不是正则匹配模式。
正则中还有一些特殊的匹配模式 \d, \s, 和 \w, 它们匹配数字,空白和单词字符。在 ASCII 模式里正则里等同 [0-9], [ \t\n\r\v] 和 [a-zA-Z0-9], 但是在 Unicode 模式里 \w 匹配一个字。
如果我们把这几个字母变成大写 \D, \S, 和 \W, 那么意味着匹配模式相反。比如: \D 匹配非数字。
import re string1 = "python 2017!"
string2 = "1,00,867!"
string3 = "!@#?"
pattern = r"(\D+\d)" match1 = re.match(pattern,string1)
match2 = re.match(pattern,string2)
match3 = re.match(pattern,string3) if match1:
print(match1.group())
print("match 1") if match2:
print(match2.group())
print("match 2") if match3:
print(match3.group())
print("match 3")
运行结果:
>>>
python 2
match 1
>>>
(\D+\d) 意味着匹配一个或者多个非数字后面跟随一个数字。
特殊匹配
还有一些特殊的匹配表达式 \A, \Z, 和 \b。\A 仅匹配字符串的开始,在大多数条件下,它的作用等同于在模式中使用 ^。 \Z 仅匹配字符串的结束,在大多数情况下,相等于 $。
\b 匹配一个词的边界。一个词的边界就是一个词不被另外一个词跟随的位置或者不是另一个词汇字符前边的位置。相当于\w 和 \W 之间有个一个空字符串。
\B 匹配一个非单词边界。它匹配一个前后字符都是相同类型的位置:都是单词或者都不是单词。一个字符串的开始和结尾都被认为是非单词。
import re string1 = "The dog eat!"
string2 = "<dog>dog<>?"
string3 = "dogeatpython"
pattern = r"\b(dog)\b" search1 = re.search(pattern,string1)
search2 = re.search(pattern,string2)
search3 = re.search(pattern,string3) if search1:
print(search1.group())
print("search 1") if search2:
print(search2.group())
print("search 2") if search3:
print(search3.group())
print("search 3")
运行结果:
>>>
dog
search 1
dog
search 2
>>>
注意:一个匹配词的边界并不包含在匹配的内容中,换句话说,一个匹配的词的边界的内容的长度是0。\b(dog)\b 匹配的结果是 "dog"。
“美满婚姻并非 “壁人成双”,而是不完美的一双学会互相欣赏彼此的差别。” -- 大卫·鲍伊
Python学习手册之捕获组和特殊匹配字符串的更多相关文章
- Python学习手册之正则表达式示例--邮箱地址提取
在上一篇文章中,我们介绍了 Python 的捕获组和特殊匹配字符串,现在我们介绍 Python 的正则表达式使用示例.查看上一篇文章请点击:https://www.cnblogs.com/dustma ...
- Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘
Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:g7v1 作者简介 作为全球Python培训界的领军人物,<Python学习手册:第4版>作者M ...
- Python学习手册(第4版)PDF高清完整版免费下载|百度云盘
Python学习手册(第4版)PDF高清完整版免费下载|百度云盘 提取码:z6il 内容简介 Google和YouTube由于Python的高可适应性.易于维护以及适合于快速开发而采用它.如果你想要编 ...
- [python学习手册-笔记]003.数值类型
003.数值类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明作者和出 ...
- 《Python学习手册》读书笔记
之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...
- 《Python学习手册》读书笔记【转载】
转载:http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html 之前为了编写一个svm分词的程序而简单学了下Python,觉 ...
- 转载-《Python学习手册》读书笔记
转载-<Python学习手册>读书笔记 http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html
- global语句(python学习手册422页)
# -*- coding: cp936 -*- #python 27 #xiaodeng #global语句(python学习手册422页) #实际上就是一个名为__builtin__的模块,但是必须 ...
- 《Python学习手册》(二)
<Python学习手册>(二) --类型和运算 数字 十六进制 八进制 二进制 0x 0o 0b hex() oct() bin() >>>int('10',2) 2 & ...
随机推荐
- error C2027: use of undefined type 'COleDispatchImpl'的解决方法
解决办法:在资源管理视图中删除CMDTARG.CPP文件,然后重新编译 设置断点后,F5调试运行,调试运行后,然后关编辑器提示保存对CMDTARG.CPP的修改,点了保存,出现error C2027: ...
- AtCoder Grand Contest
一句话题解 QwQ主要是因为这篇文章写的有点长……有时候要找某一个题可能不是很好找,所以写了这个东西. 具体的题意.题解和代码可以再往下翻._(:з」∠)_ AGC 001 C:枚举中点/中边. D: ...
- .net mvc 路由
Asp.net Mvc之Action如何传多个参数 在Global.asax文件中,默认路由如下. routes.MapRoute( "Default", // 路由名称 &quo ...
- Leetcode225 用栈实现队列
大众思路: 用两个栈实现,记为s1,s2 1.元素入栈时,加入s1 2.元素出栈时,对s2进行判断,如果s2为空,则将全部s1元素弹出并压入到s2,然后从s2栈顶弹出一个元素:如果s2不为空,则直接从 ...
- Servlet映射的过程
1.首先通过上图 locolhost:8080/login.html 访问到这个登录的html页 2 通过html页的 action="LoginServlet" 进行映射,所以填 ...
- mongodb副本集优先级设置
在设置mongodb副本集时,Primary节点.second节点,仲裁节点,有可能资源配置(CPU或者内存)不均衡,所以要求某些节点不能成为Primary我们知道mongodb的设置: 除了仲裁节 ...
- Lambda使用
说明 本文内容来自 [Java8 In Action] 一书 四种方法引用类型 类型 示例 引用静态方法 ContainingClass::staticMethodName 引用某个对象的实例方法 c ...
- 【题解】洛谷P4145 花神游历各国(线段树)
洛谷P4145:https://www.luogu.org/problemnew/show/P4145 思路 这道题的重点在于sqrt(1)=1 一个限制条件 与正常线段树不同的是区间修改为开方 那么 ...
- 【转载】iPhone屏幕尺寸、分辨率及适配
iPhone屏幕尺寸.分辨率及适配 转载http://m.blog.csdn.net/article/details?id=42174937 1.iPhone尺寸规格 iPhone 整机宽度Width ...
- UITableView控件Protocell的Identifier设置 注意事项
1. 注意:如果想使用Subtitle类型的单元格,需在Storyboard中将Protocell设置为subtitle类型,且Protocell的identifier必须与ViewControll ...