为什么re.groups()不给我一个正确匹配的组任何东西? [英] Why won't re.groups() give me anything for my one correctly-matched group?
本文介绍了为什么re.groups()不给我一个正确匹配的组任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行此代码时:
print re.search(r'1', '1').groups()
我得到()
的结果.但是,.group(0)
给了我匹配.
I get a result of ()
. However, .group(0)
gives me the match.
groups()
不应该给我一些包含火柴的东西吗?
Shouldn't groups()
give me something containing the match?
更新:感谢您的回答.因此,这意味着如果我不使用任何子组进行re.search()
,我必须使用groups(0)
进行匹配?
Update: Thanks for the answers. So that means if I do re.search()
with no subgroups, I have to use groups(0)
to get a match?
推荐答案
groups is empty since you do not have any capturing groups - http://docs.python.org/library/re.html#re.MatchObject.groups. group(0) will always returns the whole text that was matched regardless of if it was captured in a group or not
已编辑.
这篇关于为什么re.groups()不给我一个正确匹配的组任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文