Search code examples
python-3.xregexpython-re

Regex capture phrase, plus word before and word after


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

Solution

  • 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 Demo

    RegEx Details:

    • (?:^|\w+\s+): Match start position or a word followed by 1+ whitespaces
    • this phrase: Match search term
    • (?:\s+\w+|$): Match end position or 1+ whitespace followed by a word