Home > other >  Detect a sentence with specific word between sentences in regex
Detect a sentence with specific word between sentences in regex

Time:11-20

I wish to remove sentence between sentences that contain the word visitation as shown here -> https://regex101.com/r/irAN8r/1

My current regexp is below.

[^\r\n\.!]*(?i)visitation*[^\r\n.] 

The text to detect is below.

`John Doe passed away Wednesday, August 18, 2021. Visitation will be Thursday, August 26, 2021 at ABC Funeral Home. Arrangements are under the direction of McDonald Funeral Home.`

The regex does detect the sentence Visitation will be Thursday, August 26, 2021 at ABC Funeral Home but it left out the last dot (.) So, when I removed the sentence, I'm left with two dots as below...

John Doe passed away Wednesday, August 18, 2021..Arrangements are under the direction of McDonald Funeral Home.

But if I use this regexp below...

[^\r\n\.!]*(?i)visitation*[^\r\n] 

...it will detect the second and third sentence as depicted here -> https://regex101.com/r/m5G7Bk/1

Visitation will be Thursday, August 26, 2021 at ABC Funeral Home. Arrangements are under the direction of McDonald Funeral Home.

So, how can I detect the middle sentence only and with the full stop as well?

CodePudding user response:

You can use [^\r\n.!?] to match any char but CR, LF and any non-final punctuation, and [.!?]? to match the sentence end punctuation:

(?i)[^\r\n.!?]*visitation[^\r\n.!?]*[.!?]?

See the regex demo.

Details:

  • (?i) - case insensitive matching on
  • [^\r\n.!?]* - zero or more chars other than CR, LF, ., ! and ?
  • visitation - a substring
  • [^\r\n.!?]* - zero or more chars other than CR, LF, ., ! and ?
  • [.!?]? - an optional ., ! or ?.
  • Related