user2477286
user2477286

Reputation: 31

Regular expression to negate those div having class="image"

I have string in which i want to get content of only those DIV tags which does not have the class image.

I am using this regular expression:

#<\s*?div\b[^>]*class="[^image]">(.*?)</div\b[^>]*>#s

but it escapes all the DIV tags, not just the ones with the class image

Upvotes: 0

Views: 420

Answers (2)

ohaal
ohaal

Reputation: 5268

You're likely better off with a DOM parser instead.

In any case, here's a regex that should do what you're after:

<div[^>]+class="(?!(?:.+ )?image(?: .+)?")([^"]+)"

Demo: http://rubular.com/r/eekxdFdmFR

Upvotes: 1

Stocki
Stocki

Reputation: 473

The expression [^image] will find only those classes that do not contain i AND m etc. letters. Maybe negative lookahead can do the trick:

#<\s*?div\b[^>]class=\"(?:(?!image).)*\">(.?)*>#s

It will dismiss the class="images" etc. too, but I hope this is a good beginning. :)

PS: I don't know the last ] is necessary or not, but it is missing from my regexp.

Upvotes: 0

Related Questions