in reply to Re^2: Stupid question about regex with string followed variable in replacement
in thread Stupid question about regex with string followed variable in replacement

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

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

Replies are listed 'Best First'.
Re^4: Stupid question about regex with string followed variable in replacement
by LanX (Saint) on May 20, 2013 at 00:37 UTC
    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)

Re^4: Stupid question about regex with string followed variable in replacement
by Anonymous Monk on May 19, 2013 at 23:21 UTC

    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 :)