Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm really quite confused here. Hopefully, someone can give some idea of what I'm doing wrong. Here is the snippet of code I'm using:
if ($plat =~ /[^a-z]/) { print "Nothing here.\n"; $change = $false; } if ($change) { print "Platform: $plat*\n"; }
Output of my program:
Platform: *

Doesn't that really suggest that there was no lowercase letter in the variable $plat????
sometimes it works right and I'll get the intended result:
Nothing here.

Any ideas are appreciated.

Replies are listed 'Best First'.
Re: regex hell
by davido (Cardinal) on Mar 01, 2006 at 01:48 UTC

    You're mistaking character class negation for "doesn't match".

    [^a-z] means "Match a single character that is not a through z." But it doesn't mean "target may not contain a-z." If that's your intention, you might accomplish it like this:

    if( $plat !~ m/[a-z]/ ) { ....

    Or...

    if( $plat =~ m/^[^a-z]*$/ ) { ...

    The former simply says "If target doesn't match a-z." The second says, "If target matches a string that contains any amount of anything except for a-z."


    Dave

      Your second example is horrible. Say not /[a-z]/, never /^[^a-z]*$/.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: regex hell
by GrandFather (Saint) on Mar 01, 2006 at 01:02 UTC

    I'm pretty confused too. I have very little idea what you are trying to achieve. However if you modify the code below and test strings to demonstrate your problem, maybe we can help you further?

    Note that the regex you specify matches any string in which there is at least one character which is not a lower case alpha character.

    use strict; use warnings; my $change = 1; while (my $plat = <DATA>) { chomp $plat; print "$plat: "; if ($plat =~ /[^a-z]/) { print "Nothing here."; $change = 0; } if ($change) { print "Platform: $plat*"; } print "\n"; } __DATA__ OS/x os/x OS/X win WIN Win

    Prints:

    OS/x: Nothing here. os/x: Nothing here. OS/X: Nothing here. win: WIN: Nothing here. Win: Nothing here.

    DWIM is Perl's answer to Gödel
Re: regex hell
by ysth (Canon) on Mar 01, 2006 at 09:45 UTC
    Show more of your code? Where is $change ever set true? What is $false?

    Also, try:

    $plat = "asdfb\tssf"; use Data::Dumper; $Data::Dumper::Useqq=1; $Data::Dumper::Terse=1; print "Platform: ", Dumper($plat), "\n";
    to see what's in $plat in a more definitive way.