in reply to regex in REPLACEMENT in s///

($res = $str) =~ s{ (\d+) }{ ($adj = $1) =~ s/(\d)/3/g; $adj }xe;

or

($res = $str) =~ s{ (\d+) }{ $1 =~ s/(\d)/3/gr }xe;

Replies are listed 'Best First'.
Re^2: regex in REPLACEMENT in s///
by ikegami (Patriarch) on Sep 12, 2023 at 22:53 UTC

    Aye, /r (introduced in 5.14) is key here.

    You can use /r on the s/// too.

    my $res = $str =~ s{ (\d+) }{ $1 =~ s/(\d)/3/gr }xer;
    And we don't need those captures.
    my $res = $str =~ s{ \d+ }{ $& =~ s/\d/3/gr }xer;

      I probably would have gone for

      $res = $str =~ s{ \d+ }{ $& =~ tr//3/cr }xer;

      or

      $res = $str =~ s{ \d+ }{ 3 x length $& }xer;

      TMTOWTDI gone wild :)

        Well, I saw it as an exercise in using a nested s///. If you don't, then you might as well use:

        my $res = $str =~ s/\d/3/gr;
Re^2: regex in REPLACEMENT in s///
by perlboy_emeritus (Scribe) on Sep 12, 2023 at 20:13 UTC

    Thank you tybalt89; I get it :-) The REPLACEMENT expression is a perl script, and the last expression evaluated is the script's return value. I did $adj; to be grammatically correct, although plain $adj works. As I said in my post, I have much use for this idiom. The /r is also spot on with $1. Thanks again.