Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Regex error when [] occurs in file..

by moritz (Cardinal)
on Mar 03, 2008 at 15:38 UTC ( [id://671666]=note: print w/replies, xml ) Need Help??


in reply to Regex error when [] occurs in file..

if ($temp =~ /#/){ $temp =~ s/#$'//; }

That's what's causing the problem: $' can contain arbitrary data, but you try to treat it as a regex.

The "good" solution is to use this regex instead: $temp =~ s/#.*$//;

In general you can also quote interpolated variables, then they are treated as text, not as regexes:

my $varaible = '[a-z]'; m/\Q$variable\E/ # matches literal [a-z], not a character class.

If you're not inside a regex, quotemeta does the same job.

Replies are listed 'Best First'.
Re^2: Regex error when [] occurs in file..
by CountZero (Bishop) on Mar 03, 2008 at 17:12 UTC
    If you would happen to have the '#' character inside a (single or double) quoted string in your config file (I don't know if the specs for your config-file even allow this) then the s/#.*$// regex will cause you trouble as it will delete all of the string starting with the '#' character. That is probably not what you want.

    It is not easy to take care of this: not even Regexp::Common gets it right.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found