Click here to get back home

complete pattern matching

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
complete pattern matching Ela 03-22-2008
Posted by Ela on March 22, 2008, 2:10 am
Please log in for more thread options

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) ?



Posted by Martien Verbruggen on March 22, 2008, 2:59 am
Please log in for more thread options
On Sat, 22 Mar 2008 14:10:47 +0800,
>
> 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.
|

Posted by Jürgen Exner on March 22, 2008, 10:04 am
Please log in for more thread options
>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

Similar ThreadsPosted
Help with Pattern matching. Matching multiple lines from while reading from a file. May 2, 2007, 11:37 pm
Pattern Matching December 15, 2004, 10:51 am
[PATTERN MATCHING] December 17, 2004, 12:14 pm
[PATTERN MATCHING] December 20, 2004, 8:32 am
pattern matching January 26, 2005, 8:10 am
Pattern Matching March 17, 2005, 1:09 pm
pattern matching.... June 27, 2005, 12:00 pm
Help With Pattern Matching July 4, 2005, 4:27 pm
pattern matching April 20, 2006, 7:15 am
pattern matching May 23, 2006, 11:23 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap