mdunnbass has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,

So, I'm scrolling through an HTML page, and I'm trying to catch nested tags, i.e., a <span> followed by another <span> before the next </span>.

My question is, if I set up something like:

$i = 0; while ($html =~ m/</g) { # if the next character is the letter 's', $i++ # if the next character is the forward slash '/', $i-- # if $i = 1, $temp = everything from < to the next > # if $i > 1, do action to get rid of nested loop. }

First off, how do I specify 'the next character'? Is something like:
$i = 0; while ($html =~ m/</g) { if ($_ =~ m/$1s/ {$i++;} if ($_ =~ m/$1\//) {$i--;} if ($i = 1) {$temp = everything from < to the next > } if ($i > 1) {do action to get rid of nested loop} }

correct?

Secondly, is this how you would do it? Is there a better way?

Thanks
Matt

Replies are listed 'Best First'.
Re: Brief question: m// and nested HTML tags
by fenLisesi (Priest) on Nov 28, 2006 at 15:27 UTC
Re: Brief question: m// and nested HTML tags
by andyford (Curate) on Nov 28, 2006 at 15:28 UTC
Re: Brief question: m// and nested HTML tags
by hardburn (Abbot) on Nov 28, 2006 at 15:28 UTC

    I think you'll go to a lot of work that could be done more simply by a proper HTML/XML parser.


    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.