views:

275

answers:

2

I'm trying to retrieve specific tags with their content out of an xhtml document, but it's matching the wrong ending tags.

In the following content:

<cache_namespace name="content">
    <content_block id="15">
    some content here

        <cache_namespace name="user">
            <content_block id="welcome">
            Welcome Apikot!
            </content_block>
        </cache_namespace>
    </content_block>
</cache_namespace>

The content_block ending tag for id="welcome" actually get's matched as the ending tag of the first opening content_block tag.

The regex I'm using is:

/<content_block id="(.*)">([\w\W]*?)<\/content_block>/i

Any pointers as to where I'm failing?

+6  A: 

… and the answer is always the same: HTML + regex cannot be done. Sorry. Use an HTML parsing library for your particular framework. Or, if your document is guaranteed to only contain valid XHTML, take the XPath approach as proposed by jitter in a comment.

Konrad Rudolph
i was about to answer the same. wondering how many times this has been asked and answered before. and if these kind of questions should be marked duplicate.
ax
Thanks. I've been so concentrated on getting it to work that I've completely overseen the fact that it might just not work.
Andrei Serdeliuc
Depending on what language/regex-flavour you are using there you might be able to hack it with ‘recursive expressions’. But really, indeed, regex it totally the wrong tool for parsing HTML.
bobince
@bobince: true. But this still assumes valid XML which is a bit of a stretch for most HTML, unfortunately. For HTML, the only way to go is really to transform the document into some kind of DOM and then operating on it.
Konrad Rudolph
+2  A: 

This is a known problem with regex - you can't match pairs. Matching is either greedy, in which it matches the last one it finds, or non-greedy, in which it matches the first. You can't persuade a regex to count opening and closing brackets.

I would recommend loading it into a DOM and using that. If you are trying to implement an HTML parser, I would recommend using regex to lex it, then a left-right parser to parse the output of your lexer.

Paul Butcher