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

2024-04-28 11:50:20 发布

您现在位置:Python中文网/ 问答频道 /正文

re.search()的情况下,有没有一种方法可以只获取输入字符串中与regex匹配的部分?i、 e.我只想要"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'

Tags: 方法字符串importre内容searchmatchwith
1条回答
网友
1楼 · 发布于 2024-04-28 11:50:20

^{}是匹配的字符串。你知道吗

演示:

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

也可以省略参数,0是默认值。你知道吗

相关问题 更多 >