用法介绍
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-8
import re
s = "[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 = 16
m.group(1) => 'this is a book', start = 1, end = 15
m.group(2) => 'book', start = 11, end = 15
m.group(3) => 'book', start = 11, end = 15
m.group(4) => ' ', start = 10, end = 11
m.group(1, 2) => 'this',' '
m.group(1, 2) => 'is',' '
m.group(1, 2) => 'a',' '
m.group(1, 2) => 'book','None'