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 matchwidth="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.
精彩评论