views:

107

answers:

2

Hi,

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".

  • 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

+2  A: 

You need to use a negative lookahead:

width="(?!500)([^"]+)"
Andy E
Thanks Andy, that worked great.
David
A: 

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.

Jens