in reply to Converting span tags.

You could just do it iteratively instead of recursively (also assuming this goes in the one-time quick-n-dirty clean-up category) .. i.e. find all the inner <span ...>foo</span> instances (w/the help of a negative look-ahead--seeperlre) and replace them .. now repeat that until no more matches.
my $s = do {local $/=undef; <DATA>}; while( $s =~ s#<span style="(font-weight: bold;|font-style: italic;|te +xt-decoration: underline;)">(?!.*?<span)(.*?)</span>#span2tag($1,$2)# +sgei ){}; print $s; sub span2tag { my ($attr, $s) = @_; return "<b>$s</b>" if $attr =~ /bold/; return "<i>$s</i>" if $attr =~ /italic/; return "<u>$s</u>" if $attr =~ /underline/; return $s; } __DATA__ this <span style="font-weight: bold;">is</span> some <span style="font-weight: bold;">test <span style="font-style: italic;">text</span> <span style="text-decoration: underline;">for</span> bolding</span>, + underlining and italicizing text.<br />
Update: Probably a little less efficient, but clearer code:
my $s = do {local $/=undef; <DATA>}; while(1){ my $matched = 0; $matched ||= $s =~ s#<span style="font-weight: bold;">(?!.*?<span)(. +*?)</span>#<b>$1</b>#sgi; $matched ||= $s =~ s#<span style="font-style: italic;">(?!.*?<span)( +.*?)</span>#<i>$1</i>#sgi; $matched ||= $s =~ s#<span style="text-decoration: underline;">(?!.* +?<span)(.*?)</span>#<u>$1</u>#sgi; last unless $matched; }

Replies are listed 'Best First'.
Re^2: Converting span tags.
by the_0ne (Pilgrim) on Mar 27, 2006 at 20:05 UTC
    Thanks again davidrw, this is what I changed it to...

    my $s = do {local $/=undef; <DATA>}; while( $s =~ s#<span style="([^"]+)">(?!.*?<span)(.*?)</span>#span2tag +($1,$2)#sgei ){}; sub span2tag { my ($attr, $s) = @_; if ($attr =~ /bold/) { $s = "<b>$s</b>"; } if ($attr =~ /italic/) { $s = "<i>$s</i>"; } if ($attr =~ /underline/) { $s = "<u>$s</u>"; } return $s; } __DATA__ this <span style="font-style: italic; font-weight: bold;">is</span> so +me this <span style="font-weight: bold;">is</span> some <span style="font-weight: bold; text-decoration: underline;">test <span style="font-style: italic;">text</span> <span style="text-decoration: underline;">for</span> bolding</span>, + underlining and italicizing
    That will take care of nested formats.
Re^2: Converting span tags.
by the_0ne (Pilgrim) on Mar 27, 2006 at 17:28 UTC
    Thanks a lot davidrw, that sample code seemed to work perfectly. I'll mess with it some more because my example didn't include that there could possibly be more than one format per span tag. Your code helped me out a lot though, thanks again.