fbpx

Regular Expression to find term in and surrounding words

Use this regular expression to highlight a term in the middle of a sentence:

(?:\S+\s+){0,4}(money is good)\s*(?:\S+\b\s*){0,4}

Here is a great site to test the above regular expression:

https://regex101.com/r/4nhkHk/1/

References:
https://stackoverflow.com/questions/49688926/extract-few-words-before-and-after-the-specific-word-regex-in-python
https://stackoverflow.com/questions/22521282/finding-snippets-of-occurrences-of-key-words-in-text-using-perl

Share this article

Share on facebook
Share on twitter
Share on linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top