Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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*\]/;

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?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2016-10-25 21:44 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (330 votes). Check out past polls.