David
David

Reputation: 45

Negative word matching with regular expressions

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 width parameters where width does not equal "500".

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

Upvotes: 2

Views: 1835

Answers (2)

Andy E
Andy E

Reputation: 344713

You need to use a negative lookahead:

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

Upvotes: 2

Jens
Jens

Reputation: 25583

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.

Upvotes: 1

Related Questions