开发者

A problem with regex (non wanted end) [duplicate]

This question already has answers here: How do you parse and process HTML/XML in PHP? (31 answers) How to parse HTML with PHP? [duplicate] Closed 8 year开发者_如何转开发s ago.

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??


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜