#!/usr/bin/perl use warnings; use strict; my $company = 'this is a test'; my $match = $company; while ($company =~ /(this)(?=(.*?is))/g) { my $got = $1.$2; $match = $got if length($got) < length($match); } print "Shortest possible match is '$match' \n";