in reply to nested replace

If you want the dot to match anything including newlines, use the /s modifier. To evaluate the replacement as code, use the /e modifier.
use Syntax::Construct qr{ /r }; $string =~ s:/ \* (.*?) \*/ : $1 =~ s/[^\n]//gr :gsex;

In older Perls without /r :

$string =~ s:/ \* (.*?) \*/ : (my $x = $1) =~ s/[^\n]//g; $x :gsex;
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: nested replace
by Anonymous Monk on Mar 18, 2016 at 09:47 UTC

    Thanks choroba, for your comments of the modifier. I used:

    $string =~ s:/ \* (.*?) \*/ : (my $x = $1) =~ s/[^\n]//g; $x :gsex;
    but it replaces the text within the "/*...*/" with the a position of the text.
    ">/*x*/<" is converted in ">1<" instead of "><" ">/*x\nx\nx*/<" is converted in ">3<" instead of ">\n\n<"
      Weird, works for me in 5.8.3, 5.10.1, 5.20.1, and 5.22.1.
      $ perl -lwe ' $string = q<a/*z*/b>; $string =~ s: /\* (.*?) \*/ : (my $x = $1) =~ s/[^\n]//g +; $x :gsex; print $string; ' ab
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      You probably forgot the final "; $x" in the replacement part of the outer regex. Without it, the inner substitution returns the number of times it matched.

      Sorry !

      Your code works correct ! Thanks!!

      In my code I did not have the last $x

      $string =~ s:/ \* (.*?) \*/ : (my $x = $1) =~ s/[^\n]//g; $x :gsex; ^^