in reply to parsing HTML

m#<([^"'>]+|"[^"]*"|'[^']*')*>#

will match (if I'm not missing something) properly formed HTML/XML tags (as well as other similar things, which is okay because such other similar things shouldn't be found in proper HTML/XML).

To return the matched tag in $1, use (or put the parens inside the <> as I do below since that part of the tag is constant): m#(<(?:[^"'>]+|"[^"]*"|'[^']*')*>)# Note that this doesn't handle HTML comments. To properly handle both HTML comments and HTML tags using regexes, you have to march along the text like a real parser:

while( $html !~ m#\G$#gc ) { if( $html =~ m#\G([^&<]+)#gc ) { # $1 is plain text } elsif( $html =~ m#\G<!--(.*?)-->#gc ) { # $1 is a comment # I think HTML comments are defined by the standard # to actually be more complex than that, but the # practical definition appears to match the above. } elsif( $html =~ m#\G<((?:[^"'>]+|"[^"]*"|'[^']*')*)>#gc ) { # $1 is the inside of a tag } elsif( $html =~ m#\G&(\w+);#gc ) { # $1 is the name of an entity } elsif( $html =~ m!\G&#(\w+);!gc ) { # $1 is the number of an entity } else { # We have hit invalid HTML. # You can try to be lenient here if you like: if( $html =~ m#\G([&<])#gc ) { # Treat like a &amp; or &lt; if you like } else { die "Impossible??"; } } }
I almost find it hard to believe that someone writing a module to parse HTML using just Perl didn't get that much correct... or maybe I can. (:

Updated, but not much.

        - tye (but my friends call me "Tye")