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

What is the regular expression for matching everything except two specific strings?

I want a regular expression to match everything but two strings, STRING1 and STRING2.

^(?!STRING1|STRING2).*$ is close, but it does not match STRING1X, which should be matched, because STRING1X is not equal to STRING1 and not equal to STRING2.

^(?!STRING1|STRING2)$ doesn’t match anything.

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

>Solution :

^(?!STRING1$|STRING2$).*

Explanation:

^ asserts the start of the string.

(?!STRING1$|STRING2$) is a negative lookahead assertion that excludes matches for the exact strings "STRING1" and "STRING2". The $ anchor ensures that the match includes the entire string.

.* matches any character (except newline) zero or more times.

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