Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I negate an exact match in regex?

Tags:

regex

If the user enters exactly "null", I want the regex match to fail. Entering "xxxnullxxx" is ok however.

The following regex rejects "null" but it also rejects any string containing "null", which I don't want.

^(?!.*null).*$
like image 767
Jin Kim Avatar asked Oct 24 '25 08:10

Jin Kim


1 Answers

Add $ and remove .* from the look ahead:

^(?!null$).*

The trailing $ isn't needed.

like image 51
Bohemian Avatar answered Oct 27 '25 02:10

Bohemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!