Perhaps you need to be a bit more specific on the definition of your matching patterns. You said
first five letters are any letters in @group1, should it be rephrased like
first five letters should be any letters in @group1, but can contain other letters not in @group2? Otherwise I see that your example works well for patterns with first five characters only from @group1 and next two characters only from @group2.
@group1 = ('A','B','C','D');
@group2 = ('E','F','G','H');
$string='ABDCDEFABDECAFBBCDAGH';
while ($string =~ /([@group1]{5}[@group2]{2})/g)
{
print "$1\n";
}
And the output:
ABDCDEF
BBCDAGH