Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for !(A|B) e.g not (a or b )

Tags:

regex

I am using Vb.net and have following three lines and need to pick only third one leaving first and second. I tried !(REQUIRED|ESTIMATED) but it did't work.

My reg expression is !(REQUIRED|ESTIMATED) also tried but did
not work ^(REQUIRED|ESTIMATED)

09359109359REQUIRED 00000000103332014022841000099999999900000000063140000000018570
09359109359ESTIMATED00000000130452014030453000000001435000000000038560000000018570
09359109359999999999000000000058671
like image 269
ATHER Avatar asked Sep 13 '25 23:09

ATHER


1 Answers

Regex doesn't really have a not operator like you're trying to use it. If you want to match strings that don't contain a certain word/words, you probably shouldn't use regex.

If you really want to though, for whatever reason, here's how you would solve your problem with it. You have to use a negative lookahead to check for either A or B. If A and B both aren't found, then match the whole string. This looks like this:

^(?!.*(REQUIRED|ESTIMATE)).*

The ^ symbol matches the start of the line. The (?!.*(REQUIRED|ESTIMATE)) looks ahead to see if either REQUIRED or ESTIMATE is anywhere ahead, and if so, causes to match to fail. The .* matches the rest of the string if it doesn't fail.

like image 73
kabb Avatar answered Sep 15 '25 16:09

kabb