Shortest possible match : Greedy « Regular Expression « Perl






Shortest possible match

    

#!/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";

   
    
    
    
  








Related examples in the same category

1.Greedy Matches
2.Greedy and non-greedy quantifiers
3.Greedy searches
4.The Greedy Metacharacters
5.The greedy quantifier
6.Turning Off Greediness
7.A greedy quantifier
8.Shortcut Expansion Description
9.Shortcuts for Regular Expressions