my $str = "abc def ghi abc 123 junk"; my $short; while ($str =~ /abc.*?123/g) { $short = $& if ! defined $short or length($&) < length($short); pos($str) = $-[0]+1; } print "$short\n";