in reply to Removing multiple newlines from a line using regex

while ($foo =~ s/(\([^)]*)\n(.*?\))/\1\2/gs) {}

The /\1\2/ in the double quoted string should be /$1$2/.    Using \1 and \2 in double quoted strings is deprecated.

The usual form for that is:

1 while $foo =~ s/(\([^)]*)\n(.*?\))/$1$2/gs;

And you could do that without a while loop:

$foo =~ s{ \( ( [^()]* ) \) }{ ( my $x = $1 ) =~ tr/\n//d; $x }gex;

Replies are listed 'Best First'.
Re^2: Removing multiple newlines from a line using regex
by CowboyTim (Initiate) on Jul 26, 2011 at 21:34 UTC

    Hi,

    Maybe I kindof understand the problem wrongly, but wouldn't it be more simple to use two substitution regexes like this:

    $foo =~ s/\n//gs && $foo =~ s/\)/)\n/g;