Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Syntactic Confectionery Delight
 
PerlMonks  

regular expressions with !

by egunnar (Sexton)
on Sep 14, 2006 at 14:11 UTC ( #572926=perlquestion: print w/ replies, xml ) Need Help??
egunnar has asked for the wisdom of the Perl Monks concerning the following question:

I don't understand why the code doesn't print "no match" and then "no match" instead of "match" and "no match". I'm probably missing something obvious here but I"m really confused. Thanks for help. Regards, Erik
#!/usr/bin/perl -w use strict; my $regex = qr/foo/; my $test_string= 'bar'; if (! $test_string =~ /$regex/){ print "no match\n"; }else{ print "match\n"; } unless ( $test_string =~ /$regex/){ print "no match\n"; }else{ print "match\n"; }

Comment on regular expressions with !
Download Code
Re: regular expressions with !
by ozone (Friar) on Sep 14, 2006 at 14:19 UTC
    try using 'not' instead of '!', like so:

    ... if(not $test_string =~ /$regex/) { ... } ...

    It's basically a question of operator precedence.

    To make your intentions clearer, you should probably use the '!~' operator:

    ... if($test_string !~ /$regex/) { ... } ...

      It is considered good style to mark such a massive edit of a node as you have done here...either strike old content out, or at least leave a note like Edit: ....

      regards,
      tomte


      An intellectual is someone whose mind watches itself.
      -- Albert Camus

Re: regular expressions with !
by svenXY (Deacon) on Sep 14, 2006 at 14:26 UTC
    Hi,
    yes, 'not' works better:
    #!/usr/bin/perl use strict; my $regex = 'foo'; my $test_string= 'bar'; print 'no match 1',"\n" if not $test_string =~ /$regex/; print 'no match 2 ',"\n" if ! $test_string =~ /$regex/; print 'no match 3', "\n" unless $test_string =~ /$regex/;
    returns:
    no match 1 no match 3

    Regards,
    svenXY
Re: regular expressions with !
by zigdon (Deacon) on Sep 14, 2006 at 14:32 UTC
    As others mentioned, you need to use either 'not' or '()' - since '!' has a higher order of precedence than '=~'. From perlop:
    Operator Precedence and Associativity ... right ! ~ \ and unary + and - left =~ !~ ... right not

    -- zigdon

Re: regular expressions with !
by explorer (Chaplain) on Sep 14, 2006 at 14:32 UTC

    If you want "no match", use it:

    if ( $test_string !~ /$regex/) {
    unless ( $test_string =~ /$regex/) {
      Or even unless ( ! ( $test_string !~ /$regex/ ) ) {. Once you've taken the first step down the unless road, madness ensues. Don't use unless; it makes it too easy to misunderstand code.

        Don't use unless; it makes it too easy to misunderstand code.

        I dislike overgeneralizations like this, personally. The following code is perfectly clear:

        do_something() unless $big_honkin_error;

        I'm not saying that you can't avoid unless and make the code still clear, of course:

        do_something() if not $big_honkin_error:

        but the unless version is just fine.

        I wouldn't use unless at the beginning of a condition, however. But that's related to putting the important parts at the beginning of the statement as per perlstyle recommendations. Specifically:

        Just because you *CAN* do something a particular way doesn't mean that you *SHOULD* do it that way. Perl is designed to give you several ways to do anything, so consider picking the most readable one. For instance

        PrivoxyWindowOpen(FOO,$foo) || die "Can't open $foo: $!";

        is better than

        die "Can't open $foo: $!" unless open(FOO,$foo);

        because the second way hides the main point of the statement in a modifier.

Re: regular expressions with !
by Tomte (Priest) on Sep 14, 2006 at 14:34 UTC

    You're a victim of operator precedence - the ! has higher precedence than =~ - so your expression evaluates as if ((!$test_string)=~/$regex/), which is false.

    Either use !~, as already suggested, or use parenthesis around the whole match if (!($test_string =~/$regex/)).

    You do not need to use the /e modifier!

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

      Hi,
      thanks Tomte++! The only explanation that helped me understnad the problem right away!
      Regards,
      svenXY

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-04-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (555 votes), past polls