Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex match start with keyword, end with keyword or newline

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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"

>Solution :

You may use:

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

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading