Python中的 re.MatchObject.groupdict()函数– 正则表达式
此方法返回一个字典,其中组名作为键,匹配的字符串作为该键的值。
Syntax: re.MatchObject.groupdict()
Return: A dictionary with groupnames as the keys and matched string as the value for the key.
AttributeError: If a matching pattern is not found then it raise AttributeError.
考虑下面的例子:
示例 1:
用于创建和打印详细字典的程序,该字典将由用户名、网站和域组成。
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'(?P\w+)@(?P\w+)\.(?P\w+)', 'jon@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
The ?P in '()'(the round brackets) is
used to capture subgroups of strings satisfying
the above condition and the groupname is
specified in the ''(angle brackets)in this
case its Username."""
# generating a dictionary from the given emailID
details = match_object.groupdict()
# printing the dictionary
print(details)
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'(?P\w+)@(?P\w+)\.(?P\w+)', '1234567890')
""" 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
The ?P in '()'(the round brackets) is
used to capture subgroups of strings satisfying
the above condition and the groupname is
specified in the ''(angle brackets)in this
case its Username."""
# Following line will raise AttributeError exception
print(match_object.groupdict())
输出:
{‘Username’: ‘jon’, ‘Website’: ‘geekforgeeks’, ‘Domain’: ‘org’}
是时候理解上面的程序了。我们使用re.match()方法在给定字符串(' jon@geekforgeeks.org ') 中查找匹配项,' w ' 表示我们正在搜索字母字符,' + ' 表示我们正在搜索给定字符串中的连续字母字符。注意使用' () '括号来定义不同的子组,在上面的例子中,我们在匹配模式中有三个子组。 ' ?P ' 语法用于定义用于捕获特定组的组名。我们得到的结果是一个re.MatchObject ,它存储在 match_object 中。
要了解更多关于正则表达式模式的信息,请访问这篇文章。 Python正则表达式
示例 2:如果未找到匹配对象,则会引发 AttributeError。
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'(?P\w+)@(?P\w+)\.(?P\w+)', '1234567890')
""" 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
The ?P in '()'(the round brackets) is
used to capture subgroups of strings satisfying
the above condition and the groupname is
specified in the ''(angle brackets)in this
case its Username."""
# Following line will raise AttributeError exception
print(match_object.groupdict())
输出:
Traceback (most recent call last):
File "/home/fae2ec2e63d04a63d590c2e93802a002.py", line 21, in
print(match_object.groupdict())
AttributeError: 'NoneType' object has no attribute 'groupdict'