my $string = "an apple in the fridge"; my @regexes = ("an apple", "apple in a fridge", qr/(?:pine)?apples/); my %matches; for my $regex (@regexes) { my @matches = $string =~ /$regex/; for my $match (@matches) { $matches{$match} = 1; # or increment to keep score } } my @matches_sorted_by_length = sort {length($a) <=> length($b)} keys %matches; print "Longest match is $matches_sorted_by_length[-1]\n";