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 to match a string which does NOT have some words before @ symbol

I want to discard a string if some words are present before the character @ in it.

Example:
I want words foo and bar that should NOT be present on a string before @ symbol but the same words are allowed after @ symbol.

I do NOT want these:

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

abcfooxyz@domain.com

barabc@domain.com

barabc@domainfoo.com

I want these:

abcxyz@domainfoo.com

pqrabc@domainbar.com

pqrabc@domain.com


I have regex ^((?!(foo|bar)).)*$, but it also discards strings that have foo and bar after @ symbol even if the words are NOt present before @.

I only want it to discard if foo or bar is present before @ symbol in a string.


Do you have an idea how can I do it?
Thanks.

>Solution :

You need to assert at each character position preceding the @ that it is not the start of foo or bar:

^((?!foo|bar).)+@.*$

Demo on regex101

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