Python3正则表达式(4)
正则表示式的子模式
子模式的扩展语法

案例1
telNumber = 'Suppose my Phone No. is 0535-1234567, yours is 010-12345678, his is 025-87654321.'
pattern = re.compile(r'(\d{3,4})-(\d{7,8})')
pattern.findall(telNumber)
[('0535', '1234567'), ('010', '12345678'), ('025', '87654321')]
match对象的主要方法

案例2
m = re.match(r'(\w+) (\w+)', "Hello Bright, Good!")
print('返回整个模式内容:{0}'.format(m.group(0)))
print('返回第一个子模式内容:{0}'.format(m.group(1)))
print('返回第二个子模式内容:{0}'.format(m.group(2)))
print('返回指定的多个子模式内容:{0}'.format(m.group(1, 2)))
返回整个模式内容:Hello Bright
返回第一个子模式内容:Hello
返回第二个子模式内容:Bright
返回指定的多个子模式内容:('Hello', 'Bright')
案例3
exampleString = """There shoule be one -- and preferably only one -- obvious way to do it.
Althought that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is offten better than right now.""" pattern1 = re.compile(r'(?<=\w\s)never')
matchResult1 = pattern1.search(exampleString)
print('查找句子末尾的单词在整个字符串中跨度:{0}'.format(matchResult1.span())) pattern2 = re.compile(r'(?:is\s)better(\sthan)')
matchResult2 = pattern2.search(exampleString)
print('查找句子末尾的单词在整个字符串中跨度:{0}'.format(matchResult2.span()))
print('查找整个模式:{0}'.format(matchResult2.group(0)))
print('查找第一个字模式:{0}'.format(matchResult2.group(1)))
结果:
查找句子末尾的单词在整个字符串中跨度:(160, 165)
查找句子末尾的单词在整个字符串中跨度:(145, 159)
查找整个模式:is better than
查找第一个字模式: than
案例4
m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", 'Malcolm Reynolds')
print('使用子模式名字打印第1个:{0}'.format(m.group('first_name')))
print('使用子模式名字打印第2个:{0}'.format(m.group('last_name')))
结果
使用子模式名字打印第1个:Malcolm
使用子模式名字打印第2个:Reynolds
案例5
m = re.match(r'(\d+)\.(\d+)', "24.1456")
m.groups()
结果
('24', '1456')
案例6
m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", 'Malcolm Reynolds')
m.groupdict()
结果
{'first_name': 'Malcolm', 'last_name': 'Reynolds'}
案例7
exampleString = """There shoule be one -- and preferably only one -- obvious way to do it.
Althought that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is offten better than right now."""
pattern = re.compile(r'(?<=\w\s)never(?=\s\w)') # 查找不在句子开头和结尾的never
matchResult = pattern.search(exampleString)
matchResult.span()
结果
(176, 181)
案例8

Python3正则表达式(4)的更多相关文章
- 详解 Python3 正则表达式(五)
上一篇:详解 Python3 正则表达式(四) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 非捕获组和命名 ...
- 详解 Python3 正则表达式(四)
上一篇:详解 Python3 正则表达式(三) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 更多强大的功能 ...
- 详解 Python3 正则表达式(三)
上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...
- 详解 Python3 正则表达式(二)
上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...
- 详解 Python3 正则表达式(一)
本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...
- python025 Python3 正则表达式
Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...
- python3 正则表达式学习笔记
re.match函数 re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none. ~匹配成功re.match方法返回一个匹配的对象,否则返回No ...
- Python3正则表达式
正则表达式是一个特殊的字符序列,他能帮助你方便的检查一个字符串是否与某种模式匹配. re.match函数 re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,matc ...
- python3正则表达式总结
转自csdn,去这里看更多吧: https://blog.csdn.net/weixin_40136018/article/details/81183504 还有一个废话很多的详细系列,在这里:htt ...
- 【转】Python3 正则表达式特殊符号及用法(详细列表)
转载自鱼c论坛:https://fishc.com.cn/forum.php?mod=viewthread&tid=57691&extra=page%3D1%26filter%3Dty ...
随机推荐
- JS禁止鼠标右键、禁止全选、复制、粘贴的方法(所谓的防盗功能)
简述:一个防君子不防小人的鸡肋的功能,针对小白还行. 代码如下: <script> //都能支持 document.oncontextmenu = function (e) { retur ...
- 005_git专题
一.仓库管理 ➜ gittest git:(master) git config --local user.name "arunguang" ➜ gittest git:(mast ...
- VS2017编译boost库
1.http://www.boost.org/ 下载boost库. 2.解压到 D:\ProgramFiles\boost 3.环境配变量配置 VS2017更加注重跨平台性,安装文件较 ...
- linux下使用screen和ping命令对网络质量进行监控
linux下使用screen和ping命令对网络质量进行监控 场景:应用连接云服务器经常偶尔会出现连接不上的情况,android和IOS端连接的时候也会出现tcp延时5秒以上,现在想验证是否是云服务商 ...
- svn数据库自动备份脚本
创建一个存放备份数据的路径 mkdir /data/svnbak -p 采用shell脚本的方式实现自动备份 #vim backup.sh #!/bin/bash log="/data/sv ...
- 目标检测--Spatial pyramid pooling in deep convolutional networks for visual recognition(PAMI, 2015)
Spatial pyramid pooling in deep convolutional networks for visual recognition 作者: Kaiming He, Xiangy ...
- OracleOCP认证 之 Linux基础
Linux 基础 一.SHELL 1: Shell 简介 shell 是用户和Linux 操作系统之间的接口.Linux 中有多种shell, 其中缺省使用的是bash. Linux 系统的shell ...
- html----属性操作
1.文本 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的名称 - 如: red‘’RGBA() 2.水平对齐方式 text-align 属性规定元素中 ...
- 性能测试六:jmeter进阶之Cookie与header管理器
一.http cookie管理器 可以在浏览器中抓取到cookie信息,然后通过http cookie管理器为http请求添加cookie信息 添加cookie管理器后,Jmeter可以自动处理coo ...
- linux常用软件安装,常用命令
jdk [root@localhost]# tar -zxvf jdk-8u144-linux-x64.tar.gz [root@localhost]# vi /etc/profile 在profil ...