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

wise monks, I seek knowledge... In a regex substitution, how to do you follow a backreference with a literal numeric value?

Example

/some search criteria/$1$2$3[2010]$4$5/g'

thanks

Replies are listed 'Best First'.
Re: backreference followed by literal in regex substitution
by kennethk (Abbot) on Jan 05, 2010 at 18:01 UTC
    You can separate a variable name from following alphanumerics using curly brackets, a la:

    #!/usr/bin/perl use strict; use warnings; $_ = q{some search criteria}; s/(s)(o)(m)(e)( )search criteria/$1$2${3}2010$4$5/g; print;

    See Regexp Quote Like Operators for details on regular expression interpolation.

      Perfect. And Thanks! jw

Re: backreference followed by literal in regex substitution
by merlyn (Sage) on Jan 05, 2010 at 17:52 UTC
    /some search criteria/$1$2$3\[2010\]$4$5/g'

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.