python获取正则表达式匹配的字符串部分

时间:2013-12-05 19:30:01

标签: python regex search

re.search()的情况下,有没有办法可以抓住与正则表达式匹配的输入字符串部分?即我只想要"heeehe"部分,而不是之前的东西:

>>> s = "i got away with it, heeehe"
>>> import re
>>> match = re.search("he*he", s)
>>> match.string
'i got away with it, heeehe'
>>> match.?
'heeehe'

1 个答案:

答案 0 :(得分:4)

match.group(0)是匹配的字符串。

演示:

>>> import re
>>> s = "i got away with it, heeehe"
>>> match = re.search("he*he", s)
>>> match.group(0)
'heeehe'

您也可以省略参数,0是默认值。