in reply to Re: Stupid question about regex with string followed variable in replacement ( ${varname} )
in thread Stupid question about regex with string followed variable in replacement

Thank you very much! :)

Work like a charm for both problems :)

  • Comment on Re^2: Stupid question about regex with string followed variable in replacement

Replies are listed 'Best First'.
Re^3: Stupid question about regex with string followed variable in replacement
by NetWallah (Canon) on May 19, 2013 at 23:06 UTC
    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

      In this case I wouldn't use '\Q' at all, since it has an effect while '\E' is quite neutral!

      DB<100> $var=666 => 666 DB<101> "$var\Efoo" => "666foo" DB<102> "$var\Ef\Eoo" => "666foo"

      Anyway the use case is restricted to interpolation like in qq{} or regex.

      Curlies can be used for any variable.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      Thank you for your reply as well :)

      Quite interesting way to avoid that problem.

      Btw, I was using escape characters before, but like you pointed it's not working for many cases, so I finally decide to ask for Perl Wisdom to do it right :)

Re^3: Stupid question about regex with string followed variable in replacement
by LanX (Saint) on May 19, 2013 at 22:52 UTC
    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)

      And huge thanks for update! :)

      I was searching for something like that before asking my question and even after your answer without any useful results (most likely I just wrongly formulate my request).

        > (most likely I just wrongly formulate my request).

        Again, don't worry! =)

        After fruitless searching in perlvar and perlsyn I just did a googlesearch for perldoc variable curly braces name which led me to perldata

        As you can see I already needed to know the answer to find it quickly ...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        update

        Of course in hindsight perldata is the right place to look for variable syntax