Before seeing your code, I had tried something similar but limited only to $1. Now I've worked on it a bit more and came up with this. Just another WTDI.
use warnings; use strict; my $user_defined_string = "There's more than one way to do it (more th +an one)."; my $user_defined_search = '(more)(.*?)(one)'; my $user_defined_replace = '<b>$1</b>$2<b>$3</b>'; my (@subs) = $user_defined_string =~ /$user_defined_search/; for my $sub (1..@subs) { $user_defined_replace =~ s/\$$sub/$subs[$sub-1]/ge; } print "mangled replace: $user_defined_replace\n"; $user_defined_string =~ s/$user_defined_search/$user_defined_replace/g +e; print "after: $user_defined_string\n"; __OUTPUT__ mangled replace: <b>more</b> than <b>one</b> after: There's <b>more</b> than <b>one</b> way to do it (<b>more</b> t +han <b>one</b>).
As we can see, the user is expected to have a deep understanding of Perl regexes (non-greediness in this example) if she wants to do fancy stuff ;^).
--
David Serrano
In reply to Re^2: How to do regex backreferences within $variable replacement text?
by Hue-Bond
in thread How to do regex backreferences within $variable replacement text?
by ManFromNeptune
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |