x=re.search('(?<=abc).*(?=ghi)','abcdefghi').group(0)
print(x)
output
def
Explanation
Regex
(?<=abc) #Positive look behind. Start match after abc
.* #Collect everything that matches the look behind and look ahead conditions
(?=ghi) #Positive look ahead. Match only chars that come before ghi
re.search
documentation here.
A Match Object
is returned by re.search
. A group(0)
call on it would return the full match. Detail on Match Object
can be found here.
Note:
The regex is aggressive so would match/return defghixyz
in abcdefghixyzghi
.
See demo here.
solved Find a part of a string using python? [closed]