Regex capture phrase, plus word before and word after
Posted by:
adcertpoint
(Moderator)
Date: July 22, 2021 07:18PM
With Python re, I'm trying to capture a phrase, plus the word before and word after in a single return.
I.e. From the sentence...
We want to see this phrase here and then again!
Return
see this phrase here
The closest I have gotten is ...
>>> s = 'We want to see this phrase here and then again!'
>>> re.search("\w*\sthis phrase\w*\s",s)
<_sre.SRE_Match object; span=(11, 27), match='see this phrase '>