Try
Regexp::List. It converts
qw/foobar fooxar foozap fooza/ to
qr/foo(?:[bx]ar|zap?)/ using Trie structure so that the resulting regex is much more efficient than naive
/foobar|fooxar|.../. And
tillys approach to counting each word by capturing the match sounds great.