Your skill will accomplish
what the force of many cannot
Interpolation of capture buffers not working when regex stored in variableby jacklh (Initiate)
|on Jun 05, 2013 at 20:01 UTC||Need Help??|
jacklh has asked for the
wisdom of the Perl Monks concerning the following question:
I'm trying to add functionality to an existing script that sends files to customers. The problem is we have dozens, possibly hundreds in near future, of customers who want the files we send them to match their specific naming convention. As such, I'm trying to make our script generic and allow a series of regex patterns to be defined in an INI per client. We have it working EXCEPT for capture groups, which are not interpolating at all--it always remains as $1, $2 ... $n. Here is a script showing the basic concept of what we've tried (and failed) when dealing with capture groups.
Results when hardcoded into script:
Results when using ARGV method as "perl pmtest.pl $2$1":
Here is another conceptual version illustrating the issue we've run into.
The test.ini config file
The results: Methods 1 - 3 (not working)
Methods 4 - 5 (not working)
Method 6 works, but hardcoded:
Any ideas to make capture groups work when patterns defined in INI?
(1) I know the test.ini format leaves security risk of code injection--the above script is just for illustrative purposes. In the prod script, we actually just parse the before and after regex patterns from the INI and build the correct substitution regex.
(2) I consulted these posts, which provided valuable insight (and used in above methods) and ALMOST have the solution, but either don't work for capture groups defined in INI or when it does work, it's too rigid by hardcoding the number of capture groups in source code rather than allowing it to be dynamically defined in an INI, again necessary because we may potentially be holding hundreds of unique client configurations and need this to scale: