re.match()返回<_sre.SRE_Match object; span=(0, 5), match=’trfgr’>的处理方法:
加上.group(0)
例子:
import re
string=’trfgr1gr1gr1111gpoyg_tgpcmndu’
pattern=”…gr”
result1=re.match(pattern,string)#从开头匹配一个模式–只返回一个结果
result2=re.match(pattern,string).span()#过滤掉一些信息,只留位置,返回元组
print(result1.group(0))#注意:返回str
print(result1)#注意:返回<_sre.SRE_Match object; span=(0, 5), match=’trfgr’>
print(result2)
本文介绍了如何使用Python正则表达式的re.match()方法,并通过示例详细解释了如何获取匹配结果的位置信息及如何将匹配结果转换为字符串。
459

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



