Search code examples
regexinversesentence

Regular Expression to select all sentences Except one sentence with specific word


I want to select all sentences which do not includes "Bull & Cat" word.

This is a Dog. This is Cat. This is a Bull. This is a Cow

((?<=^)|(?<=\.))\s*(?![^.]*Bull).*?(?:\.|$)((?<=$)|(?<=\.))

This regex selects sentences except one that contain Bull, but I need to select all sentences other than contains Bull & Cat.

Edit the above regex. So, i can select all sentences except those who contains Bull & Cat.


Solution

  • Here you go:

    ((?<=^)|(?<=\.))\s*(?![^.]*Bull).*?(?:\.|$)((?<=$)|(?<=\.))

    Demo: https://regex101.com/r/Y3gerF/1

    The above regex will include the ending dots at the end of each sentence inside the match. If you don't mind not getting those dots, you can simplify a little bit to:

    ((?<=^)|(?<=\.))\s*(?![^.]*Bull).*?((?=$)|(?=\.))

    Demo: https://regex101.com/r/wzq4Sr/1