#!/usr/bin/env perl use strict; use warnings; my @tokens = qw/report_time report_day reset read/; my @abbrev; for my $token (@tokens) { if (1 == grep /^$token/, @tokens) { push @abbrev, $token; } else { die "Error: $token is not unique in list of tokens\n"; } # Generate abbreviations that match only one token my $abbrev = $token; while (1) { chop($abbrev); last unless (1 == grep /^$abbrev/, @tokens); push @abbrev, $abbrev; } } # Show the abbreviations print "Abbreviations = (" . join(',', sort @abbrev) . ")\n"; # Create the regex string to use my $regex = join('|', sort @abbrev); print "Token regex = \"$regex\"\n\n"; # Read the file and output tokens while (<>) { my $match; if (($match) = m/^($regex)$/) { my @matches = grep /^$match/, @tokens; print "$match matched for @matches on line $.\n"; } } exit;