Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > finding the first match in a string

Reply
Thread Tools

finding the first match in a string

 
 
size14feet@gmail.com
Guest
Posts: n/a
 
      04-09-2005
must simple way to do this...

I have a string. I have several patterns to match for. I'd like
to determine which pattern finds a match in the string first & at what
position.

Anyone have an elegant way to do this? I don't mind using a CPAN
module, if need be.


much appreciated-
matt

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      04-09-2005
<> wrote:

> I have a string. I have several patterns to match for. I'd like
> to determine which pattern finds a match in the string first & at what
> position.
>
> Anyone have an elegant way to do this?



--------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $string = 'foo TX bar 12345 baz 98765-1234';

my %patterns = (
street => qr/\d+\s+.*/,
zip => qr/\d{5}(-\d{4})?/,
state => qr/\b[A-Z][A-Z]\b/,
);

print "$string\n";
print "01234567890123456789\n";

foreach my $type ( keys %patterns ) {
print "$type at ", length($1), "\n" if $string =~ /(.*?)$patterns{$type}/;
}
--------------------------------

Making note of the lowest value of length($1) is left as
an exercise for the reader.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      04-09-2005
wrote:

> I have a string. I have several patterns to match for. I'd like
> to determine which pattern finds a match in the string first &
> at what position.


You want the special variable @-

You have to make sure the patterns themselves contain no capturing
(...) but they can, of course, contain grouping parentheses (?:...).

if ( /(pat1)|(pat2)|(pat3)/ ) {
print "Pattern $#- matched at position $-[0]\n";
}

Note because of the way the regex engine works this may be
significantly less efficient than checking each pattern separately in a
loop if you have a large number of patterns.

 
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
How to check first x characters match a specific string Angus C Programming 16 11-18-2010 06:28 AM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57