In the case of re.search()
, is there a way I can get hold of just the part of input string that matches the regex? i.e. I just want the "heeehe"
part and not the stuff that comes before it:
JavaScript
x
8
1
>>> s = "i got away with it, heeehe"
2
>>> import re
3
>>> match = re.search("he*he", s)
4
>>> match.string
5
'i got away with it, heeehe'
6
>>> match.?
7
'heeehe'
8
Advertisement
Answer
match.group(0)
is the matched string.
Demo:
JavaScript
1
6
1
>>> import re
2
>>> s = "i got away with it, heeehe"
3
>>> match = re.search("he*he", s)
4
>>> match.group(0)
5
'heeehe'
6
You can also omit the argument, 0
is the default.