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 string but do not match when containing certain elements

I am trying to make a regex match with a series of similar strings but not match others that meet another certain criteria. I almost got it right but am having trouble when trying to exclude certain name words of a random length.

And I am trying to parse a full string from the middle part, here are some examples.

Examples of strings I want to match and what to extract from them:

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

You see a tall athletic dark-brown-haired adult man with dark olive skin and hazel eyes.
You see a short fat yellow-haired youthful man with pale skin and blue eyes.

I want to extract these from the above examples:

tall athletic dark-brown-haired adult man with dark olive skin and hazel eyes
short fat yellow-haired youthful man with pale skin and blue eyes

Examples of strings I DO NOT want to match and just disregard: (anything ending in (yourself!) or , whom you know as )

You see a masked tall-height heavy man with white skin (yourself!).
You see a furrow-browed older man with cropped pepper-gray hair, whom you know as Richard.
You see a younger man with cropped blue hair, whom you know as Bob.

I have a limited understanding of regex and these are above me. Any help would be appreciated.

What I have so far (partially working):

^You see (a|an) (.*?)(?<!, whom you know as Steve.|\(yourself!\).)$

>Solution :

You could shorten (a|an) to an? if you don’t want to capture that part separately.

The capture group for the part that you are interested (.*) in does not have to be non greedy, as it should end right before the dot at the end of the string.

Excluding the lines using a negative lookahead (?!, instead of a negative lookbehind:

^You see an? (?!.*(?:, whom you know as\b|\(yourself!\)))(.*)\.$

Regex 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