I've created a regular expression that can extract sentences containing a matched word.
[^.|?|!]*\<friends\>[^.|!|?]*[\"!?:\.]
But, it does not apply to cases where there are Mr./Mrs./ Dr. etc in the sentence.
For example:
The adventures are great. I don't know whether you know that Dr. Watson and Mr. Holmes are good friends, Ms. Adler.
My desired output is:
I don't know whether you know that Dr. Watson and Mr. Holmes are good friends, Ms. Adler.
How to do this?
Through negative lookahead.
(?:(?!Mr|Ms|Dr|[.?!]).|Mr\.|Ms\.|Dr\.)*\bfriends\b(?:(?!Mr|Ms|Dr|[.?!]).|Mr\.|Ms\.|Dr\.)*[\"!?:.]
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With