Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > complete pattern matching

Reply
Thread Tools

complete pattern matching

 
 
Ela
Guest
Posts: n/a
 
      03-22-2008

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


 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      03-22-2008
On Sat, 22 Mar 2008 14:10:47 +0800,
Ela <(E-Mail Removed)> 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.
|
 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      03-22-2008
On Sat, 22 Mar 2008 14:10:47 +0800,
Ela <(E-Mail Removed)> 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.
|
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-22-2008
"Ela" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Matching neighbouring words of a pattern using Regex CV Perl 2 08-31-2004 12:27 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM
A newbie question on pattern matching DelphiDude Perl 3 07-26-2003 12:54 PM



Advertisments