$text = '1 + 2 + dfdf'; for( split '\s', $text ){ if(//.../<\/maths>/){ next }else{ if(/+/){ $text =~ s/$_/ + / } } } #STDOUT: 1  +  2 + dfdf