complete pattern matching

E

Ela

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

Martien Verbruggen

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
 
M

Martien Verbruggen

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
 
J

Jürgen Exner

Ela said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top