Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Does not contain sub string [duplicate]

I want to test for strings that do not contain a particular sub string

Examples strings are:

event2
event23,event1,event67=12
event1,event30
event23
event2
event2,event23

I want to match strings that do not contain event2.

Therefore the following strings should match:
event23,event1,event67=12
event1,event30
event23

So far I can match strings the do contain event2 with the following expression:
/^.*(\bevent2\b)/gm

But I don't know how to combine this with a negative lookahead.

This doesn't seem to work:
/^.*(?!.*(\bevent2\b))/gm


1 Answers

You could use a negative lookahead:

^(?!.*event2\b).*$

like image 99
The fourth bird Avatar answered Feb 20 '26 02:02

The fourth bird



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!