开发者

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

This question already has answers here: Regex: match everything but a specific pattern (5 answers) 开发者_运维百科 Closed 2 years ago.

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.


^(?!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".


(?!...)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜