Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

character class problem

by smackdab (Pilgrim)
on Oct 23, 2003 at 04:29 UTC ( #301478=perlquestion: print w/ replies, xml ) Need Help??
smackdab has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am reading lines from a file and getting confused on the quotes and regex stuff!!!!

I want 2a to work. I want to setup some $VALID regex rules up front and then compare them to lines in my file...
$VALID1 = '[-a-zA-Z0-9_.*\s]'; $line1a = "[no\nluck]"; $line1b = "[no\nluck]"; $line2a = '[no\nluck]'; # FROM A read(<FILE>) $line2b = '[no\nluck]'; # FROM A read(<FILE>) print "luck1a\n" if $line1a =~ /\[\s*($VALID1+)\s*\]/m; print "luck1b\n" if $line1b =~ /\[\s*([a-z\s]+)\s*\]/m; print "luck2a\n" if $line2a =~ /\[\s*($VALID1+)\s*\]/; #DESIRED print "luck2b\n" if $line2b =~ /\[\s*([a-z\s]+)\s*\]/;

Comment on character class problem
Download Code
Replies are listed 'Best First'.
Re: character class problem
by Roger (Parson) on Oct 23, 2003 at 04:54 UTC
    First of all, "[no\nluck]" and '[no\nluck]' are not the samething. \n in double quotes is expanded to return, while in single quotes is set to character \ followed by character n.

    Your regular expression 2a doesn't work because there is no character \ in the $VALID1 set.

      I guess then I need to figure out how to get a line read from a file to be "no\nluck" instead of 'no\nluck' Do i have to convert every line after I read it or is there a better way?
        I am not sure what you are trying to achieve here. Do you want to read the entire file into a single string? If so, try the following:

        use IO::File; use strict; my $f = new IO::File "data", "r" or die "can not open file"; my $data; { local $/; $data = <$f>; } # at here, $data is your entire data file with embedded \n.


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (16)
As of 2015-07-07 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls