The problem with your approach is that it will fail if someone changes the case of the tags or your tags have attributes. Let's say you have a table inside of another table and the first start table tag has attributes (or the tag is spread over more than one line), then your routine might match at the second table tag down to the last table tag, thus resulting in imbalanced tags.

For this, use a parser. In this case, since it appears that you want to remove tables inside of tables, you'll also want to keep track of the number of table start and end tags to ensure that you are properly balancing them. Here's a quick hack for you.

#!/usr/bin/perl -w use strict; use HTML::TokeParser::Simple 1.4; my $parser = HTML::TokeParser::Simple->new( *DATA ); my $html = ''; my $not_balanced = 0; while ( my $token = $parser->get_token ) { $html .= $token->as_is unless $not_balanced; if ( $token->is_tag('table') ) { $not_balanced += $token->is_start_tag ? 1 : -1; # ugh, I don't like the double negative $html .= $token->as_is if $token->is_end_tag and ! $not_balanced; } } print $html; __DATA__ <p>one</p> <table> <tr> <td> <table> <tr> <td>test</td> </tr> </table> </td> </tr> </table> <p>two</p>

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)


In reply to Re: Regular Expression by Ovid
in thread Regular Expression 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.