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 ...
随机推荐
- 【vim】保存文件没有权限 :w !sudo tee %
每当你打开一个你没有写入权限的文件(比如系统配置文件)并做了一些修改,Vim 无法通过普通的 ":w" 命令来保存. 你不需要重新以 root 方式打开文件再进行修改,只需要运行: ...
- 【vim】保存文件并退出 :w=:wq
这两个命令实际上并不完全等价,当文件被修改时两个命令时相同的.但如果未被修改,使用 :x 不会更改文件的修改时间,而使用 :wq 会改变文件的修改时间.
- V4L2学习记录【转】
转自:http://blog.chinaunix.net/uid-30254565-id-5637600.html V4L2学习记录 这个还没有分析完,先在这放着,防止电脑坏掉丢了,以后再完善 V4L ...
- C++:vector中的v.at(0)和v[0]的区别
设v是一个vector的对象, 如果v是非空的,则v.at(0)和v[0]是没有区别的,都是取数组中第一个值: 如果v是空的,则v.at(0)会抛出异常(exception std::out_of_r ...
- (并发编程)线程 (理论-创建-lock-属性-守护,与进程的对比)
一.线程理论1.什么是线程 线程指的是一条流水线的工作过程(执行代码) 进程不是执行单位,是资源单位 一个进程内自带一个线程,线程是执行单位 2.进程VS线程 1.同一进程内的线程们 ...
- zabbix通过简单shell命令监控elasticsearch集群状态
简单命令监控elasticsearch集群状态 原理: 使用curl命令模拟访问任意一个es节点可以反馈的集群状态,集群的状态需要为green curl -sXGET http://serverip: ...
- dell r720服务器raid5安装centos6.5系统
服务器型号R720 已经配置好了raid,需要安装centos6.5系统 1.开机,按F10,进入系统引导界面,选择加载系统选项,并选择redhat 6.7选项 系统提示不支持,选择仍然继续,根据提示 ...
- android项目结构
- 设置滚动条scrolltop
scrolltop用来设置页面的滚动条的位置 兼容性:链接 $().scrolltop(值)
- Python 多环境配置管理
一.概述 实际工程开发中常常会对开发.测试和生产等不同环境配置不同的数据库环境,传统方式可以通过添加不同环境的配置文件达到部署时的动态切换的效果.这种方式还不错,不过不同环境间往往会共享相同的配置而造 ...