Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: "ee" in Regular Expression: version issue?

by Kenosis (Priest)
on Dec 03, 2012 at 19:52 UTC ( #1006936=note: print w/ replies, xml ) Need Help??


in reply to "ee" in Regular Expression: version issue?

Try concatenating the vars for use in the substitution:

use strict; use warnings; my $lhs = '(\d*?)-(.*?)-(.*)'; my $rhs = '$1.$2.$3'; while ( my $var = <DATA> ) { chomp $var; $var =~ s/$lhs/$rhs/ee; print $var, "\n"; } __DATA__ 123-456-789 -456-789 789

Output:

123456789 456789 789

From the error message, it appears that the results of the first evaluation is ambiguous to the interpreter when it attempts to evaluate it without the concatenation operator.


Comment on Re: "ee" in Regular Expression: version issue?
Select or Download Code
Re^2: "ee" in Regular Expression: version issue?
by kennethk (Monsignor) on Dec 03, 2012 at 20:23 UTC

    Or use straight interpolation: my $rhs = '"$1$2$3"';. The key here is that the string literal present after the initial eval (which subs $rhs to its value) is valid Perl code.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Yes, excellent. Perhaps the enclosing double-quotes (also) helps to disambiguate the expression. After the first eval, the interpreter sees this:

      $var =~ s/(\d*?)-(.*?)-(.*)/$1$2$3/ee;

      The substituting expression is problematic w/o either being interpolated via "$1$2$3" or concatenated using $1.$2.$3.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006936]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-07-12 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls