开发者

Negative word matching with regular expressions

开发者 https://www.devze.com 2023-01-04 13:34 出处:网络
I want to be able to construct a regular expression which searches for a particular pattern in some HTML code where one parameter is negated (i.e. find x where y is NOT present).

I want to be able to construct a regular expression which searches for a particular pattern in some HTML code where one parameter is negated (i.e. find x where y is NOT present).

Example: I want to find image 开发者_Go百科width parameters where width does not equal "500".

  • width="640" height="360" would match

  • width="500" height="360" would NOT match

I'm using a search & replace plugin for wordpress to run the regular expression - http://urbangiraffe.com/plugins/search-regex - it just uses a generic regex syntax

I'm able to match simple queries but I'm afraid negation is a bit beyond me - any help would be much appreciated.

Thanks - David


You need to use a negative lookahead:

width="(?!500)([^"]+)"


The regex way to do negation would be negative lookaheads, see here. This would look like

<img [^>]*width=(?!"500")

But it would be far easier and less errorprone to not use regex and use a HTML parser instead.

0

精彩评论

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