in reply to Using a 'hash' of regexes, then seeing if they match based upon a 'split' mapped to an array?

I don't fully understand what you're after, but here's an approach that might be interesting. It parses the template elements in order through a string. (Actually, a full-blown parser might be what you really need.)

c:\@Work\Perl\monks>perl -wMstrict -le "my @lines = ( 'my-test 10:11:23 blah yada', 'test 11:22:33', ' taste-test 01:02:03 fie', '10:20:30 toast', '999 hooha yip yap', 'another-test 999 foo fee', ); ;; my %parse = ( '$(long_awkward.string)' => qr{ (?<! [[:alpha:]]) [[:alpha:]]+ (?: - [[:alpha:]]+)* (?! [[:al +pha:]]) }xms, '%t' => qr{ (?<! \d) \d\d : \d\d : \d\d (?! \d) }xms, ); ;; my $template = '$(long_awkward.string) %t xxx yyy zzz'; ;; for my $line (@lines) { my @sub_tmpls = (split ' ', $template)[0, 1]; my $match = validate($line, \%parse, @sub_tmpls); print qq{'$line' }, $match ? 'matches' : 'NO match'; } ;; sub validate { my ($string, $hr_parser, @sub_templates) = @_; ;; for my $st (@sub_templates) { return unless $string =~ m{ \G \s* $hr_parser->{$st} }xmsg; } return 1; } " 'my-test 10:11:23 blah yada' matches 'test 11:22:33' matches ' taste-test 01:02:03 fie' matches '10:20:30 toast' NO match '999 hooha yip yap' NO match 'another-test 999 foo fee' NO match


Give a man a fish:  <%-{-{-{-<

  • Comment on Re: Using a 'hash' of regexes, then seeing if they match based upon a 'split' mapped to an array?
  • Select or Download Code