Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks. I need a little help.

Sorry for posting this, but I can't find solution by myself. I have very stupid question about regexp with string followed variable in replacement.

For example: $1 and 0 (zero) after it. $foo with bar after it.

It's easy to avoid it, by assigning 0 and bar to variable or by placing some special character, that not usable as part of variable name, for example slash.

But is there any way to do that another way? Maybe there is some character, that could be used as separator?

N.B. Examples below completely pointless and stupid, they just to illustrate the problem.

Example of problem #1 (of course it's not working, since we don't have $foobar variable, it should be $foo variable and bar word after it):

#!/usr/bin/perl use warnings; use strict; my $txt = 'test'; my $foo = 'foo'; $txt =~ s/test/$foobar/; print "$txt\n";

"Fix" for example problem #1:

#!/usr/bin/perl use warnings; use strict; my $txt = 'test'; my $foo = 'foo'; my $fix = 'bar'; $txt =~ s/test/$foo$fix/; print "$txt\n";

Example of problem #2 (of course it's not working, since we don't have $10, it should be $1 variable and 0 (zero) after it):

#!/usr/bin/perl use warnings; use strict; my $txt = '1,2'; $txt =~ s/(\d+)\.(\d+)/$10,$2/;

"Fix" for example problem #2:

#!/usr/bin/perl use warnings; use strict; my $txt = '1,2'; my $fix = 0; $txt =~ s/(\d+)\.(\d+)/$1$fix,$2/; print "$txt\n";

Replies are listed 'Best First'.
Re: Stupid question about regex with string followed variable in replacement ( ${varname} )
by LanX (Saint) on May 19, 2013 at 22:09 UTC
    > But is there any way to do that another way? Maybe there is some character, that could be used as separator?

    Perl always allows curlies to surround the symbol's name (or expression) following sigils!

    So

    $txt =~ s/test/${foo}bar/;

    instead of

    $txt =~ s/test/$foobar/;

    HTH =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    UPDATE

    Documentation wasn't easy to find:

    from perldata#Scalar value constructors

    As in some shells, you can enclose the variable name in braces to disambiguate it from following alphanumerics (and underscores). You must also do this when interpolating a variable into a string to separate the variable name from a following double-colon or an apostrophe, since these would be otherwise treated as a package separator:

      Thank you very much! :)

      Work like a charm for both problems :)

        In the spirit of TIMTOWTDI, you could also disable metacharacter processing using "\Q..\E":
        my $txt = '1,2'; $txt=~ s/(\d+)\,(\d+)/$1\Q0\E,$2/;
        This converts the "1" to "10".

        Unfortunately, simply escaping like "\0" does not work for many cases, because many escape sequences have special meaning .. Eg: \0 starts an octal sequence. See "perldoc perlrebackslash"

                     "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
                -- Dr. Cox, Scrubs

        You're welcome! =)

        BTW it's not a stupid questions, many allowed (though exotic) variable names are only accessible if surrounded by curlies ...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Stupid question about regex with string followed variable in replacement
by Laurent_R (Canon) on May 19, 2013 at 23:18 UTC

    I fully and wholeheartedly agree with Rolf that this was not a stupid question at all (I remember having has a similar problem years ago), but your explanation of the problem could have been clearer. I did not understand at all the problem until I read your code.

      Yep, I know :(

      I was thinking how to explain it better, but without luck. So I just create sample code to make it clear.

      Sorry :)