Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - how to tell something NOT to match? [duplicate]

Tags:

regex

How can I create a regex NOT to match something? For example I want to regex to match everything that is NOT the string "www.petroules.com".

I tried [^www\.petroules\.com] but that didn't seem to work.

like image 818
Jake Petroules Avatar asked Jun 03 '10 16:06

Jake Petroules


2 Answers

^(?!www\.petroules\.com$).*$

will match any string other than www.petroules.com. This is called negative lookahead.

[^www\.petroules\.com]

means "Match one character except w, p, e, t, r, o, u, l, s or dot".

like image 60
Tim Pietzcker Avatar answered Oct 02 '22 23:10

Tim Pietzcker


(?!...)

This is called negative lookahead. It will only match if the regex ... does not match. However, note that it DOES NOT consume characters. This means that if you add anything else past the ), it will start matching right away, even characters that were part of the negative lookahead.

like image 22
Donald Miner Avatar answered Oct 02 '22 23:10

Donald Miner