>>> import re
>>> my_reg = re.compile(r'(a(b(c)))')
>>> text = "abc"
>>> mat = re.search(my_reg,text)
>>> print(mat)
<_sre.SRE_Match object at 0x101a28ae0>
>>> mat.group(0)
'abc'
>>> mat.group(1)
'abc'
>>> mat.group(2)
'bc'
>>> mat.group(3)
'c'
本文通过一个具体的Python代码示例介绍了如何使用正则表达式进行分组匹配,并展示了如何通过group方法获取不同级别的匹配结果。
>>> import re
>>> my_reg = re.compile(r'(a(b(c)))')
>>> text = "abc"
>>> mat = re.search(my_reg,text)
>>> print(mat)
<_sre.SRE_Match object at 0x101a28ae0>
>>> mat.group(0)
'abc'
>>> mat.group(1)
'abc'
>>> mat.group(2)
'bc'
>>> mat.group(3)
'c'
您可能感兴趣的与本文相关的镜像
Python3.9
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
2809
925
2419

被折叠的 条评论
为什么被折叠?