Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

regex and getpwent()...

by penguinfuz (Pilgrim)
on May 28, 2001 at 04:47 UTC ( [id://83668]=perlquestion: print w/replies, xml ) Need Help??

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

Whenever I use a specific regular expression for exclusion, I still get a value returned. Here is a snippet...
while(($name,$pass,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = getp +went()) { if(!($shell =~ m/false/ || m/null/ || m//)) { print blah... } }
The users with "false" and "" shells are excluded, but "null" shells still get printed; However, the exclusion works if I populate the variables by using split...
... while(<PW>) { ($name,$pass,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) = split(/: +/); print blah... }
Can anyone offer me a bit of insight as to what's happening here?

Replies are listed 'Best First'.
Re: regex and getpwent()...
by japhy (Canon) on May 28, 2001 at 05:01 UTC
    Your first regex is done on $shell, but the second two are done on $_. I think you want to take an approach like:
    if ($shell ne "" and $shell !~ /false|null/) { # it's ok }
    You could, of course, keep the other two regexes separate:
    if ($shell ne "" and $shell !~ /false/ and $shell !~ /null/) { # it's ok }
    Also, the pattern // does NOT match empty strings -- it uses the last matched pattern again:
    "foo" =~ /foo/; print grep //, qw( bar foobar ); # foobar
    To match empty strings, either use /^$/ or /^\z/, depending on your definition of empty.

    japhy -- Perl and Regex Hacker
Re: regex and getpwent()...
by bobione (Pilgrim) on May 28, 2001 at 05:10 UTC
    I am not sure that m// match empty ("") value. It matches everything.

    BobiOne KenoBi ;)

      It only matches everything until you've matched anything. (Think that's cryptic?)
      for (qw( a b403 c<<>_# d84930 e89293->><{}W )) { print if //; } "foo" =~ /foo/; for (qw( a b403 c<<>_# d84930 e89293->><{}W )) { print if //; }


      japhy -- Perl and Regex Hacker
        Ouch! That's quite nasty looking...
        Looks like I really need to get my head around regular expressions a bit better! 8)

        Thanks for the help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-28 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found