Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx Extract Sentence with a Matched Word, without stopping at "Mr.", "Mrs." etc

Tags:

regex

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?

like image 390
DanEng Avatar asked Oct 14 '14 09:10

DanEng


1 Answers

Through negative lookahead.

(?:(?!Mr|Ms|Dr|[.?!]).|Mr\.|Ms\.|Dr\.)*\bfriends\b(?:(?!Mr|Ms|Dr|[.?!]).|Mr\.|Ms\.|Dr\.)*[\"!?:.]

DEMO

like image 85
Avinash Raj Avatar answered Nov 24 '22 10:11

Avinash Raj