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 '>
In your regex since you're matching \w*\s
after search term it is matching 0 words and a single whitespace after your search term.
You may use this more robust regex to handle cases when your search term is at the start of a line or at the end of a line:
(?:^|\w+\s+)this phrase(?:\s+\w+|$)
RegEx Details:
(?:^|\w+\s+)
: Match start position or a word followed by 1+ whitespacesthis phrase
: Match search term(?:\s+\w+|$)
: Match end position or 1+ whitespace followed by a word