in reply to regular expression problem

Here is one solution:

$string =~ s/$regex/eval "\"$replace\""/e;

The problem with your method was that to get that to work it would have to do variable substitution twice, first to substitute $replace with '$2 $1', then a second variable substitution of $2 and $1. Using eval in connection with the e modifier basically does just this second variable substitution.

Replies are listed 'Best First'.
Re^2: regular expression problem
by johngg (Canon) on Aug 17, 2009 at 12:44 UTC

    Rather than use eval inside the replacement term you can use a double ee modifier (see the s operator in Regexp Quote Like Operators).

    $ perl -le ' > $str = q{10 09}; > $reg = q{(\d+) (\d+)}; > $rep = q{"$2 $1"}; > $str =~ s/$reg/$rep/ee; > print $str;' 09 10 $

    I hope this is of interest.

    Cheers,

    JohnGG

Re^2: regular expression problem
by perl_fan (Novice) on Aug 17, 2009 at 12:13 UTC
    Super cool
    
    Thanks you are a Hero