Double interpolation of regular expressions in Perl
Posted
by tomdee
on Stack Overflow
See other posts from Stack Overflow
or by tomdee
Published on 2009-02-09T11:33:27Z
Indexed on
2010/04/09
21:23 UTC
Read the original article
Hit count: 597
I have a Perl program that stores regular expressions in configuration files. They are in the form:
regex = ^/d+$
Elsewhere, the regex gets parsed from the file and stored in a variable - $regex
.
I then use the variable when checking the regex, e.g.
$lValid = ($valuetocheck =~ /$regex/);
I want to be able to include perl variables in the config file, e.g.
regex = ^\d+$stored_regex$
But I can't work out how to do it.
When regular expressions are parsed by Perl they get interpreted twice. First the variables are expanded, and then the the regular expression itself is parsed.
What I need is a three stage process:
First interpolate $regex
, then interpolate the variables it contains and then parse the resulting regular expression.
Both the first two interpolations need to be "regular expression aware". e.g. they should know that the string contain $
as an anchor etc...
Any ideas?
© Stack Overflow or respective owner