in reply to First Pattern Matching

The Anonymonk is close, but I prefer a solution that works without code assertions:
#!/usr/local/bin/perl -w use strict; my @pattern = ('B.B', 'CB'); my $re = qr/^(.*?)(?:@{[ join "|", map "($_)", @pattern ]})/; foreach (qw(ABCBXBCA APCBXBCAC)){ if(my ($keyword,@match) = /$re/) { my $i = 0; $i++ until defined shift @match; print "String:$_ Pattern:$pattern[$i] KeyWord:$keyword +\n"; } }

The key is that I capture separately for each pattern.

Update: per jryan's point, edited to generate the regex once before the loop.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: First Pattern Matching
by Anonymous Monk on Jul 11, 2002 at 23:56 UTC
    Hrrmm, I must admit I like this solution better than my solution. It's cleaner and simpler. Darn that "Keep it simple". ;)

    But it still suffers from having to look up the match afterwards, and that I don't like. Indeed it's a good way, but for a large @pattern and some "bad luck" you still have notably unnecessary overhead when having large lists to check against. For simple things that other people might read I'd use your method, but for a module that needs efficiency (don't all?) I'd use my way, I think.

    Cheers,
    -Anomo