📜  Python正则表达式中的 re.MatchObject.group()函数

📅  最后修改于: 2022-05-13 01:55:50.889000             🧑  作者: Mango

Python正则表达式中的 re.MatchObject.group()函数

re.MatchObject.group()方法默认返回完整匹配的子组或匹配子组的元组,具体取决于参数的数量

考虑下面的例子:

示例 1:

从 emailID 打印用户名、公司名和域的程序

Python3
import re
 
"""We create a re.MatchObject and store it in
   match_object variable
   the '()' parenthesis are used to define a
   specific group"""
 
match_object = re.match(r'(\w+)@(\w+)\.(\w+)', 'username@geekforgeeks.org')
 
""" w in above pattern stands for alphabetical character
    + is used to match a consecutive set of characters
    satisfying a given condition
    so w+ will match a consecutive set of alphabetical characters"""
 
# for entire match
print(match_object.group())
# also print(match_object.group(0)) can be used
 
# for the first parenthesized subgroup
print(match_object.group(1))
 
# for the second parenthesized subgroup
print(match_object.group(2))
 
# for the third parenthesized subgroup
print(match_object.group(3))
 
# for a tuple of all matched subgroups
print(match_object.group(1, 2, 3))


Python3
import re
 
"""We create a re.MatchObject and store it in
   match_object variable
   the '()' parenthesis are used to define a
   specific group"""
 
match_object = re.match(r'(\w+)@(\w+)\.(\w+)', 'username@geekforgeeks.org')
 
""" w in above pattern stands for alphabetical character
    + is used to match a consecutive set of characters
    satisfying a given condition
    so w+ will match a consecutive set of alphabetical characters"""
 
# Following line will raise IndexError exception
print(match_object.group(7))


输出:

username@geekforgeeks.org
username
geekforgeeks
org
('username', 'geekforgeeks', 'org')

是时候理解上面的程序了。我们使用re.match()方法在给定字符串(' username@geekforgeeks.org ') 中查找匹配项,' w ' 表示我们正在搜索字母字符,' + ' 表示我们正在搜索给定字符串中的连续字母字符。注意使用' () '括号来定义不同的子组,在上面的例子中,我们在匹配模式中有三个子组。我们得到的结果是一个re.MatchObject ,它存储在 match_object 中。

注意:要了解有关正则表达式模式的更多信息,请参阅Python regex

根据传递的参数, group 方法返回不同的字符串,它还返回匹配字符串的元组。

示例 2:

如果我们在方法参数中传递了一个无效的组号,那么我们将得到一个 IndexError 异常。

Python3

import re
 
"""We create a re.MatchObject and store it in
   match_object variable
   the '()' parenthesis are used to define a
   specific group"""
 
match_object = re.match(r'(\w+)@(\w+)\.(\w+)', 'username@geekforgeeks.org')
 
""" w in above pattern stands for alphabetical character
    + is used to match a consecutive set of characters
    satisfying a given condition
    so w+ will match a consecutive set of alphabetical characters"""
 
# Following line will raise IndexError exception
print(match_object.group(7))

输出:

Traceback (most recent call last):
  File "/home/8da42759204c98da7baa88422a4a74e0.py", line 17, in 
    print(match_object.group(7))
IndexError: no such group