use strict; use warnings; my $string = qq[FGTXYZGTFABCGHABCFGTXYZADXYZGTYABC]; #or try this my $string = qq[FGTXYZABCABC]; $string =~ m/[XYZ]{3}([A-Za-z]{0,21}?)[ABC]{3}?(?{print "matched at [<$&>] $-[0]\n" })(?!)/x;