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; }

In reply to Re: Converting span tags. by davidrw
in thread Converting span tags. by the_0ne

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.