Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

backreference followed by literal in regex substitution

by jayw (Initiate)
on Jan 05, 2010 at 17:51 UTC ( [id://815788]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://815788]
Approved by zwon
Front-paged by zwon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found