#!perl use strict; use warnings; my $string = qq[FGTXYZGTFABCGHABCFGTXYZADXYZGTYABC]; my ($pos, $nummatch) = (0, 0); while ($string =~ /^(.{$pos,}?)[XYZ]{3}([A-Za-z]{0,21}?)[ABC]{3}/) { my $tpos = $pos; $pos = length($1) + 1; $nummatch = length($2); while ($string =~ /^(.{$tpos,}?)[XYZ]{3}([A-Za-z]{$nummatch,21}?)[ABC]{3}/) { print length($1) . ', ' . length($2) . $/; $nummatch = length($2) + 1; } } __OUTPUT__ 3, 3 3, 8 20, 8 25, 3