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

Regular expression to get a string between two strings with exception

So I have this string:

My cow always gives milk

My goat sometimes gives milk

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

My giraffe always gives milk

My goat always buys milk

And this regex:

(?<=cow ).*(?= milk)|(?<=goat ).*(?= milk)

I am matching:

always gives

sometimes gives

 

always buys

I need to match:

always gives

sometimes gives

 

 

How can I add an exception to my regex to prevent matching anything containing the word "buys"? So I only get matches on the first and second line.

>Solution :

One approach would be to use a tempered dot instead of plain .* to match only words which are not buys:

(?<=\bcow )(?:(?!\bbuys\b).)*(?= milk\b)|(?<=\bgoat )(?:(?!\bbuys\b).)*(?= milk\b)

Here is a working demo.

The "new" part of my answer is the following:

(?:(?!\bbuys\b).)*

This says to match, one character at a time, anything, provided that we do not cross the word buys. So, any word will match the above pattern so long as that word is not buys. This is called a "tempered" dot, because, unlike .*, which blindly matches everything, this tempered dot excludes the word buys.

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