Here is how to use HTML::Parser to do the job (your) regex doesn't:

#!/usr/bin/perl -w package Filter; use strict; use base 'HTML::Parser'; my ($filter, %ok_tags); my @ok_tags = qw ( i b ); @ok_tags{@ok_tags} = @ok_tags; sub start { my ($self, $tag, $attr, $attrseq, $origtext) = @_; $filter .= exists $ok_tags{$tag} ? $origtext : 'x'; } sub text { my ($self, $text) = @_; $filter .= $text; } sub comment { my ($self, $comment) = @_; $filter .= $comment; } sub end { my ($self, $tag, $origtext) = @_; $filter .= $ok_tags{$tag} ? $origtext : 'x'; } my $html = join '', <DATA>; my $parser = new Filter; $parser->parse($html); $parser->eof; print $filter; __DATA__ <html> <head> <title>Foo</title> </head> <body> <B>This regex is broken</b> <b>It will cope with this</b> <b >But not this< /b> <i>told you</i>ts<b>roken</B> </body> </html>

Before you lay that on your teacher make sure you understand how the hash slice lookup table and the V2 interface to HTML::Parser works :-)

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Perl with XML by tachyon
in thread Perl with XML by Anonymous Monk

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.