in reply to Constructing a list of regexes

I thought of another question while reading these responses. If I want to read in the substitution value from a file and the substitution contains backreferences, how can I make that work? For example:
my $new_value='$2 and $1'; my $string="(Jack) and (John)"; my $regex=qr/$string/; $string=~s/$regex/$new_value/;
This obviously won't work how I intend, but how would I do this?

Replies are listed 'Best First'.
Re: Re: Constructing a list of regexes
by tall_man (Parson) on Feb 20, 2003 at 22:42 UTC
    The following will work. There was another problem with your example. "(Jack) and (John)" the literal string, does not match itself as a pattern with capturing parentheses.
    my $new_value='$2 and $1'; my $string="(Jack) and (John)"; my $regex=qr/$string/; $string = "Jack and John"; eval "\$string=~s/\$regex/$new_value/"; print $@ if $@; print "$string\n";