Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Regexp substitution using variables

by MikeTaylor (Acolyte)
on Nov 25, 2020 at 22:36 UTC ( [id://11124234]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Regexp substitution using variables
in thread Regexp substitution using variables

Well, this gets me much of what I need ... but I can't get back-references, either with $1 or \1. In either case, they appear as literals. Any ideas, other than eval?

Replies are listed 'Best First'.
Re^4: Regexp substitution using variables
by LanX (Saint) on Nov 25, 2020 at 23:56 UTC
    > but I can't get back-references,

    This works for me

    DB<44> $pat='(x)\1' DB<45> $mod='i' DB<46> 'xX' =~ /(?$mod)$pat/; say "$&:$1" xX:x DB<47>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Sorry, I think I was using the term "back-reference" incorrectly. I didn't mean referring back within the pattern to something earlier in the pattern, but referring in the substitution string to a captured sub-expression of the pattern. Something like s/(foo|bar)/He said "$1"/.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124234]
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-03-29 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found