$text =~ s {( # Capture in $1 # Tag [^<]* # Not a <, zero or more times (?: # Group, non-capture. < # <, (?!/maths>) # not followed by /maths> [^<]* # Not a <, zero or more times )* # Repeat. # End tag. ) # End capture $1. | # Or + # Target. } {$1 || " + "}exg;