use strict; use warnings; use 5.010; my $text = q{xxxAAsgdhsdgABudhwuACedAGuwydADwieuhwiudAEudwdfuw}; my $tag = shift || q{AA}; my $limit = q{ZZ}; my @records = (); my $prevPosn = index $text, $tag, 0; die qq{ERROR: First record '$tag' not found in text\n} if $prevPosn == -1; warn qq{WARNING: First record '$tag' not at start of text\n} unless $prevPosn == 0; while ( ( my $posn = index $text, ++ $tag, $prevPosn + 2 ) != -1 ) { push @records, substr $text, $prevPosn, $posn - $prevPosn; $prevPosn = $posn; last if $tag eq q{ZZ}; } push @records, substr $text, $prevPosn; say qq{>$_<} for @records;