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