while ($foo =~ s/(\([^)]*)\n(.*?\))/\1\2/gs) {} #### 1 while $foo =~ s/(\([^)]*)\n(.*?\))/$1$2/gs; #### $foo =~ s{ \( ( [^()]* ) \) }{ ( my $x = $1 ) =~ tr/\n//d; $x }gex;