Impressive, thank you! As previously threatened, and as per your comments, some notes on trying to break the regex solution ;-)

using names that can be confused with the interesting ones: <divx, aclass="data", ...

Good point, but without some trickery those would no longer validate properly as XHTML either.

using XML namespaces liberally

Indeed, I tested this and it does cause trouble: Unsurprisingly the regex and HTML parsers can't handle it, but a little more surprising is that Mojo::DOM ignores namespaces and therefore fails with the following, and that also XML::Twig has trouble with namespaces, or at least I haven't found the right options yet. Only the XML::LibXML and XML::XSH2 solutions handle this correctly:

<html xmlns:foo="http://www.w3.org/1999/xhtml" xmlns:bar="http://www.perlmonks.com" ... <foo:div class="data" id="Zero" /> <bar:div class="data" id="Hi">there</bar:div>

(Update: Hmm, even the W3C Validator is having trouble with the namespaces...)

using external entities

As noted here, even some XML parsers seem to have trouble loading all the external entities. But even entities declared within the document should make life difficult for regexes:

<!ENTITY atad "data"> ... <div class="&atad;" id="Zero" />

Only the XML::LibXML and XML::Twig solutions handle that correctly, everything else (including XML::XSH2) fails.

Looks like XML::LibXML <update> and XML::XSH2 </update> are the only ones left standing in this torture test so far! :-)

And one more thing: currently entities with hex values like &#xA0; aren't supported by the regex (although that's not too difficult to fix).

Updated since the issue with XML::XSH2 was worked out further down in this thread.


In reply to Re^2: Parsing HTML/XML with Regular Expressions by haukex
in thread Parsing HTML/XML with Regular Expressions by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.