in reply to Matching HTML Tags

I have no idea why you're trying to do this, but the following code should work as specified:
use strict; use warnings; my $str = '&nbsp; <x &nbsp;<y &nbsp;>>'; my $c = 0; my @arr; while ($str =~ /<[^<>]+>/) { $str =~ s/<([^<>]+)>/`$c`/; $arr[$c++] = $1; } s/&nbsp;/"/ig for @arr; while ($str =~ /`\d+`/) { $str =~ s/`(\d+)`/<$arr[$1]>/g; } print $str;
The hard part is dealing with nested tags, but if you start with the inside tags and work outwards, you have no real problems.

Replies are listed 'Best First'.
Re^2: Matching HTML Tags
by chas (Priest) on May 24, 2005 at 02:08 UTC
    Nice! (but s/&nbsp;/&quot;/ in your code for the present case, of course...)
    chas