in reply to Things you think you'll never use.

Why not something like this:

$re = qr{ \< (?: (?> [^<>]+ ) # Non-parens without backtracking | (??{ $re }) # Group with matching parens )* \> }x; $s = "B<I<U<bold underlined text>>> and B<bold text>"; 1 while $s =~ s{([BUI])<([^<>]*(?:$re[^<>]*)*)>}{<$1>$2</$1>}g; print $s,"\n";

Jenda
We'd like to help you learn to help yourself
Look around you, all you see are sympathetic eyes
Stroll around the grounds until you feel at home
   -- P. Simon in Mrs. Robinson