How to Match a Sentence of a Word in Regex?

You probably scratch all the internet to find a regex which will select all the sentences of your word. If you not found any solution yet, then done worry, we will give you proper guidance on this question.

We will start with an example, 

This is the sentence one containing Dog Word. This is the sentence two contain Cat word. This is the sentence three contain Bull word.

In this three sentences, imagine that you want to select sentence with word “Cat“, then your dream regex will look like that


Check live on Regex101.

Full Meaning & Execution of the regex.

Positive Lookbehind (?<=^|\s) Assert that the Regex below matches

1st Alternative ^^ asserts position at start of a line

2nd Alternative \s\s matches any whitespace character (equivalent to [\r\n\t\f\v ])

Match a single character present in the list below [A-Z] A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)

Match a single character not present in the list below [^!?.]

If you want to select sentences of multiple words. For Example Cat & Dog then use | symbol between the words.

check on regex.

For matching sentences of a specific number or characters pattern then use something like below,

Lets suppose, you want to select sentences that contain a year. So, any year contains fours numbers so you can use [0-9]{4}. This means any number that contain 0 to 9 number 4 times in a row.

Limitation of this Regex

The ending dot of the selected sentence will not be selected.

The sentence will end at punctuation marks like (?,!). If you only want to end with dot sign then remove other punctuation from the end of the regex and check on regex 101.

Leave a Comment