简体   繁体   中英

python get the part of the string matched by the regex

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? ie I just want the "heeehe" part and not the stuff that comes before it:

>>> 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'

match.group(0) is the matched string.

Demo:

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

You can also omit the argument, 0 is the default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM