1

I have the following pattern:

(name|id)\s*=\s*('|")([a-zA-Z\-\_])+('|")

And I have to get all attributes name="a" or id="ab_c" which does not have the structure name="a-element" or id="a-element" (finishes with -element), I tried with:

(name|id)\s*=\s*('|")([a-zA-Z\_][^-element])+('|")

but it does not work, what is the mistake??

José Carlos
  • 1,005
  • 16
  • 29
  • Is this string part of some XML or HTML? If so, don't parse it with a regex (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Instead, use `DOMElement` (http://php.net/manual/en/class.domelement.php) (or another XML parser). –  Sep 21 '11 at 16:25

1 Answers1

3

You want negative look-arounds, like this:

(name|id)\s*=\s*('|")([a-zA-Z\-_](?!-element))+('|")

(But keep in mind that you probably shouldn't be parsing XML manually.)

Community
  • 1
  • 1
ladenedge
  • 13,197
  • 11
  • 60
  • 117