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

Hello, I am trying to match "received 486 response" and am using $_ =~ m/received 486 response/ for this. This also ends up matching "received 480 response". How come a 6 is equal to 0. What am i missing? Thanks!

Replies are listed 'Best First'.
Re: Pattern match
by GrandFather (Saint) on Dec 05, 2014 at 01:13 UTC

    Most likely the string you are matching contains both received 486 response and received 480 response, or the match is failing but there is an issue elsewhere in your code that makes it look like the match succeeds. As already suggested, show us some runnable code that demonstrates the problem.

    Perl is the programming world's equivalent of English
Re: Pattern match
by james28909 (Deacon) on Dec 05, 2014 at 00:16 UTC
    use strict; use warnings; my $string = shift; if ( $string =~ /received 486 response/ ) { print "matched $string\n"; } else { print "did not match $string\n"; }
    Ran it with 'script.pl "received 486 response"' and it worked as intended. It does not match "received 485 response" or "received 480 response" or "received 487 response".

    EDIT: Maybe you should post some code other that the pattern match, so we can see what is going on.

    EDIT2: Here is another example:
    my @array = ( "received 480 response", "received 485 response", "received 486 response", "received 487 response" ); for (@array) { if ( $_ =~ /received 486 response/ ) { print "matched $_\n"; } else { print "did not match $_\n"; } } ________________________________________________ Output is: F:\>script.pl did not match received 480 response did not match received 485 response matched received 486 response did not match received 487 response
    So maybe what ever is inside your variable is indeed not what you think it is <.<
Re: Pattern match
by Eily (Monsignor) on Dec 04, 2014 at 23:52 UTC

    Could you provide a code sample to demonstrate your problem? (Something that we could run, that's short, and where the result is not the one you expect see How do I post a question effectively?). Because right now, the answer that can be given to you is "it doesn't.

    If you are going for an exact match, index might just do the trick for you (it's understood by more people, less side effects and unexpected behavior, faster)

      Following is a sample code: if ($_ =~ m/received 486 response to blah/){ do this; } else{ do that; } However this doesn't seem to work correctly, because when a portion of the string is "received 480 response to blah" it still matches and breaks the logic.

        when a portion of the string is "received 480 response to blah" it still matches
        Your regex does not match 'received 480 response to blah', so either your error is somewhere else in your code, or as stated by GrandFather, your string $_ contains both the 486 and 480 variations.

        When we say "runnable code", we mean something that we can copy and run, and that will actually do something. If I run: perl -e 'if ($_ =~ m/received 486 response to blah/){ do this; } else{ do that; }' nothing happens.