Home > database >  regex match start with keyword, end with keyword or newline
regex match start with keyword, end with keyword or newline

Time:11-12

Assume I have this text

Hi there how are you. This is a test for the root cause of ice cream addiction and not the category of happiness.

I would like a regex that matches the text the starts with "root cause" and ends with either:

  1. the word "category";
  2. or a newline (whichever comes first).

I've only managed to get as far as (root cause).* but can't figure out how to add the condition for "category" or "newline".

In the above example, I expect to get back "root cause of ice cream addiction and not the".

If I then have a sentence that reads

``Hi there how are you. This is a test for the root cause of ice cream addiction

and not the category of happiness.``

(note the line break), then I expect to get back "root cause of ice cream addiction"

CodePudding user response:

You may use:

\broot cause\b.*?(?:\bcategory\b|\n)

Demo

  • Related