I agree. This uses HTML::TokeParser. I have found that it is easily adaptable to do any chore you may have parsing html. Since I've started using it I've never used a regex on html. It's never worth the effort.
#!/bin/perl5
use strict;
use warnings;
use HTML::TokeParser;
open HTML_FILE, 'form.html' or die;
my $tp = HTML::TokeParser->new( \*HTML_FILE ) or die;
my $html;
my $found_form = 0;
while ( my $t = $tp->get_token ) {
$found_form++, next if $t->[0] eq 'S' and $t->[1] eq 'form';
$found_form--, next if $t->[0] eq 'E' and $t->[1] eq 'form';
next if $found_form;
$html .= $t->[4] if $t->[0] eq 'S';
$html .= $t->[1] if $t->[0] eq 'T' or $t->[0] eq 'C';
$html .= $t->[2] if $t->[0] eq 'E';
}
close HTML_FILE;
print "$html\n";
# ["S", $t, $attr, $attrseq, $text]
# ["E", $t, $text]
# ["T", $text, $is_data]
# ["C", $text]
# ["D", $text]
# ["PI", $token0, $text]
wfsp
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.