Python 正则表达式的 Match 对象的 group 方法
来源:互联网 发布:欧洲杯决赛数据 编辑:程序博客网 时间:2024/05/18 02:03
用法介绍
match.group([group1, ...])
返回 match 的一个或多个子组。
如果只有唯的一参数,返回单一的子符串;如果有多个参数,结果是对应每一个参数的元素组成的 tuple 。
如果没有参数, group1 的默认值为 0 (返回整个匹配的字符串)。
如果一个 groupN 参数的值为 0 ,对应的返回值为整个匹配的字符串;如果参数值在 1 到 99 之间(含),返回对应的括号组匹配的字符串。
如果组号参数 (groupN)为负数,或者大于定义在模式中的组的个数,会抛出一个 IndexError 异常。
如果模式中的某个组没有匹配(例如:(\w+ )?
,(\w+ )*
),对应的结果为 None 。
如果模式中的某个组匹配了多次(例如:(\w+ )+
,(\w+ )*
),将返回最后匹配的字符串。
Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned). If a groupN argument is zero, the corresponding return value is the entire matching string; if it is in the inclusive range [1..99], it is the string matching the corresponding parenthesized group. If a group number is negative or larger than the number of groups defined in the pattern, an IndexError exception is raised. If a group is contained in a part of the pattern that did not match, the corresponding result is None. If a group is contained in a part of the pattern that matched multiple times, the last match is returned.
示例
程序
# coding=utf-8import res = "[this is a book]"PATTERN = "\[(((\w+)(\s+)?)+)\]"m = re.search(PATTERN, s, re.DOTALL)if m: for i in range(5): print("m.group(%d) => '%s', start = %d, end = %d" % (i, m.group(i), m.start(i), m.end(i))) it = re.finditer("(\w+)(\s+)?", m.group(1)) for match in it: print("m.group(1, 2) => '%s','%s'" % match.group(1, 2))
运行结果
m.group(0) => '[this is a book]', start = 0, end = 16m.group(1) => 'this is a book', start = 1, end = 15m.group(2) => 'book', start = 11, end = 15m.group(3) => 'book', start = 11, end = 15m.group(4) => ' ', start = 10, end = 11m.group(1, 2) => 'this',' 'm.group(1, 2) => 'is',' 'm.group(1, 2) => 'a',' 'm.group(1, 2) => 'book','None'
参考
https://docs.python.org/3.6/library/re.html#re.match.group
- Python 正则表达式的 Match 对象的 group 方法
- java 正则表达式 Match 的group方法见解
- 正则表达式的Match,Group,Capture类
- java正则表达式的match.group(int group)参数详解
- C#正则表达式类Match和Group类的理解
- C#正则表达式类Match和Group类的理解
- Javascript正则表达式的match方法
- 详解Python正则表达式之: (?P=name) match earlier named group 匹配前面已命名的组
- 正则表达式之Group方法的用处
- String 对象中的 正则表达式方法 match
- 正则表达式的group
- python之正则表达式match()方法
- python网络爬虫学习(三)正则表达式的使用之re.match方法
- python正则表达式模块re中search和match方法的区别
- python正则表达式函数match()和search()的区别
- 正则表达式中 test(),exec(),match() 方法的区别
- 正则表达式中的exec和match方法的区别
- 正则表达式group的使用
- JavaScript Prototype 详解
- CentOS7 安装 MongoDB
- 【形状匹配算法】Shapecontext形状上下文算法的研究
- Linux 命令
- H264音视频编解码高质量博客
- Python 正则表达式的 Match 对象的 group 方法
- spring的依赖注入
- cron和crontab
- hdu1232 并查集详解
- Android调试之断点进程选择
- c++ Nullable类实现
- RabbitMQ如何在命令行下清除消息队列中的所有数据
- Selenium3 元素定位
- LapSRN 超分辨率