use warnings; use strict; my @pats = qw(ab ac ad ba bla etc); my %pos; @pos{ @pats } = 1 .. @pats; my $re = join '|', @pats; $re = qr/$re/; while () { print "$1 $pos{$1}\n" if /($re)/; } __DATA__ fable fact