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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |