Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matching a non-empty string that is not equal to certain value

I'm trying to write a regex to match any string that is not empty or equals to www.

Example:

www => don't match
ww => match
adwww => match
wwwad => match
abcde => match

My regex:

(.+)(www)

How can I fix my regex?

like image 262
Bách Nguyên Trần Avatar asked Oct 25 '25 08:10

Bách Nguyên Trần


1 Answers

You need a lookahead based regex:

^(?!www$).+

See the regex demo.

Details:

  • ^ - start of string
  • (?!www$) - the string cannot be equal to www
  • .+ - 1 or more characters (other than a line break if you do not use a DOTALL modifier)
like image 93
Wiktor Stribiżew Avatar answered Oct 27 '25 01:10

Wiktor Stribiżew



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!