Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: using variables in regular expr replace

by Joost (Canon)
on Dec 02, 2004 at 17:45 UTC ( #411880=note: print w/ replies, xml ) Need Help??


in reply to using variables in regular expr replace

You can disable regex interpretation of a supplied variable by using \Q and \E:

$line =~ s/\Q$var2\E/$var1/;
See perlre for more info.


Comment on Re: using variables in regular expr replace
Download Code
Re^2: using variables in regular expr replace
by Fletch (Chancellor) on Dec 02, 2004 at 17:57 UTC

    And just to elaborate on why you need to do this, remember that [] indicates a character class inside a regular expression (i.e. [a-c] means any of the characters a, b, or c). Your [img ... 8-4 ... ] is being interpreted as a character class, and you've got an invalid range (the character 8 is "bigger" than 4 and hence the correct way to specify the range of characters would be [4-8]).

    At any rate you really should either escape the square brackets or use \Q...\E as shown above (and you should read perldoc perlretut and perldoc perlre).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-09-17 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (85 votes), past polls