http://www.perlmonks.org?node_id=301478

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.