complete pattern matching

Discussion in 'Perl Misc' started by Ela, Mar 22, 2008.

  1. Ela

    Ela Guest

    By using the following codes:

    if ($aref1->[3] =~ /$tomatch/i) {
    print "A:$tomatch\tB:$aref1->[3]\n";
    <STDIN>;
    last;
    }

    I discover that the program gives me a match for the following pair:

    A:conserved hypothetical B:CONSERVED HYPOTHETICAL W

    Indeed I expect that is a mismatch (but not the other way round for matching
    A with B). How to enforce matching the whole expresson (i.e. including the
    "W" in this case) ?
     
    Ela, Mar 22, 2008
    #1
    1. Advertisements

  2. On Sat, 22 Mar 2008 14:10:47 +0800,
    Ela <> wrote:
    >
    > By using the following codes:
    >
    > if ($aref1->[3] =~ /$tomatch/i) {
    > print "A:$tomatch\tB:$aref1->[3]\n";
    > <STDIN>;
    > last;
    > }
    >
    > I discover that the program gives me a match for the following pair:
    >
    > A:conserved hypothetical B:CONSERVED HYPOTHETICAL W
    >
    > Indeed I expect that is a mismatch (but not the other way round for matching
    > A with B). How to enforce matching the whole expresson (i.e. including the
    > "W" in this case) ?


    Anchor it at the front and back with ^ and $, or \A and \z. See the
    perlre doucmentation for details.

    But if you want to test for equality, it might be faster to simply
    lowercase (or uppercase) both strings, and test with eq. If you know
    that one of your astrings is always entirely upper or lower case, it
    becomes even cheaper to do.

    if (lc $aref1->[3] eq lc $tomatch) { ... }

    Martien
    --
    |
    Martien Verbruggen | Freudian slip: when you say one thing but
    | mean your mother.
    |
     
    Martien Verbruggen, Mar 22, 2008
    #2
    1. Advertisements

  3. On Sat, 22 Mar 2008 14:10:47 +0800,
    Ela <> wrote:
    >
    > By using the following codes:
    >
    > if ($aref1->[3] =~ /$tomatch/i) {
    > print "A:$tomatch\tB:$aref1->[3]\n";
    > <STDIN>;
    > last;
    > }
    >
    > I discover that the program gives me a match for the following pair:
    >
    > A:conserved hypothetical B:CONSERVED HYPOTHETICAL W
    >
    > Indeed I expect that is a mismatch (but not the other way round for matching
    > A with B). How to enforce matching the whole expresson (i.e. including the
    > "W" in this case) ?


    Anchor it at the front and back with ^ and $, or \A and \z. See the
    perlre doucmentation for details.

    But if you want to test for equality, it might be faster and more
    readable to simply lowercase (or uppercase) both strings, and test with
    eq. If you know that one of your astrings is always entirely upper or
    lower case, it becomes even cheaper to do.

    if (lc $aref1->[3] eq lc $tomatch) { ... }

    Martien
    --
    |
    Martien Verbruggen | Freudian slip: when you say one thing but
    | mean your mother.
    |
     
    Martien Verbruggen, Mar 22, 2008
    #3
  4. "Ela" <> wrote:
    >By using the following codes:
    >
    >if ($aref1->[3] =~ /$tomatch/i) {
    > print "A:$tomatch\tB:$aref1->[3]\n";
    > <STDIN>;
    > last;
    >}
    >
    >I discover that the program gives me a match for the following pair:
    >
    >A:conserved hypothetical B:CONSERVED HYPOTHETICAL W
    >
    >Indeed I expect that is a mismatch (but not the other way round for matching
    >A with B). How to enforce matching the whole expresson (i.e. including the
    >"W" in this case) ?


    If you really want RE matching then just anchor the search pattern ("^" and
    "$").

    If you just want to compare the two strings then comparing ("eq") their e.g.
    lower case normal forms would be easier and faster and actually yield
    correct results even in case the pattern contains RE special characters.

    jue
     
    Jürgen Exner, Mar 22, 2008
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DelphiDude
    Replies:
    3
    Views:
    1,202
  2. danpres2k
    Replies:
    3
    Views:
    7,533
    danpres2k
    Aug 25, 2003
  3. CV
    Replies:
    2
    Views:
    617
    Charles DeRykus
    Aug 31, 2004
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    300
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    307
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page