在上一篇文章中,我们介绍了 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学习手册之捕获组和特殊匹配字符串的更多相关文章

  1. Python学习手册之正则表达式示例--邮箱地址提取

    在上一篇文章中,我们介绍了 Python 的捕获组和特殊匹配字符串,现在我们介绍 Python 的正则表达式使用示例.查看上一篇文章请点击:https://www.cnblogs.com/dustma ...

  2. Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘

    Python学习手册(第4版) - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:g7v1 作者简介 作为全球Python培训界的领军人物,<Python学习手册:第4版>作者M ...

  3. Python学习手册(第4版)PDF高清完整版免费下载|百度云盘

    Python学习手册(第4版)PDF高清完整版免费下载|百度云盘 提取码:z6il 内容简介 Google和YouTube由于Python的高可适应性.易于维护以及适合于快速开发而采用它.如果你想要编 ...

  4. [python学习手册-笔记]003.数值类型

    003.数值类型 ❝ 本系列文章是我个人学习<python学习手册(第五版)>的学习笔记,其中大部分内容为该书的总结和个人理解,小部分内容为相关知识点的扩展. 非商业用途转载请注明作者和出 ...

  5. 《Python学习手册》读书笔记

    之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...

  6. 《Python学习手册》读书笔记【转载】

    转载:http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html 之前为了编写一个svm分词的程序而简单学了下Python,觉 ...

  7. 转载-《Python学习手册》读书笔记

    转载-<Python学习手册>读书笔记 http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html

  8. global语句(python学习手册422页)

    # -*- coding: cp936 -*- #python 27 #xiaodeng #global语句(python学习手册422页) #实际上就是一个名为__builtin__的模块,但是必须 ...

  9. 《Python学习手册》(二)

    <Python学习手册>(二) --类型和运算 数字 十六进制 八进制 二进制 0x 0o 0b hex() oct() bin() >>>int('10',2) 2 & ...

随机推荐

  1. Best Time to Buy and Sell Stock II--疑惑

    https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 代码如下时能AC class Solution { publi ...

  2. ubuntu 16.04 virtualbox could not insert 'vboxdrv': Required key not available 问题解决方法

    从 内核版本 4.4.0-20 开始,在开启了 Secure Boot 的电脑上,未注册的 kernel 模块不再允许执行,所以如果想在保持 Secure Boot 的情况下依然允许执行,我们需要做的 ...

  3. TIA Portal 和 scout 之间的驱动器地址分配

    TIA Portal集成了scout.在使用simotion控制器时,分配驱动装置的地址可能会碰到问题. 解决方法: 1)在配置驱动时,TIA Portal软件的语言需要选择为应为中文 2)unico ...

  4. 一些通过SAP ABAP代码审查得出的ABAP编程最佳实践

    1. 这两个IF ELSE分支里检测的条件其实逻辑上来说都是同一类,应该合并到一个IF分支里进行检查: It is an expensive operation to open a file in a ...

  5. 08提权 系统文件权限和远程连接IP绕过 安装后门

    大家都知道08权限的系统权限设置很严格  面对限制IP连接的情况 我们及时拿到system权限 有账号也上不去这种情况下只能弄shift后门 或者放大镜了  但08权限 在system权限也操作不了系 ...

  6. WAS上配置数据源连接失败

    问题描述: 在节点 cnshh171Node01 上的服务器 server1 上, 对数据源 testj2cbug 执行的测试连接操作 由于以下异常 java.sql.SQLException: 调用 ...

  7. Ajax向Controller发送请求并接受数据需要注意的一个细节

    想用Ajax想向Controller发送请求和接收返回的字符等等.Controller中要使用@ResponseBody注解. <script type="text/javascrip ...

  8. Codeforces 962D - Merge Equals

    链接: http://codeforces.com/problemset/problem/962/D 题意: 给出一个整数序列.选择其中最小且出现两次(或以上)的数,把最左边的两个从序列中移除,然后把 ...

  9. [USACO08FEB]酒店Hotel

    嘟嘟嘟 这道题以前在学校内网刷过类似的,AC了后还挺有成就感,所以更详细的题解请看这里. 总的来说,就是用线段树维护区间最长连续0.因此我们要维护这么几个值:lmax:从当前区间左端点开始最长的连续0 ...

  10. localStorage和cookie操作

    localStorage和cookie操作代码: cookie: { isSupportCookie: function() { return navigator.cookieEnabled; }, ...