#!/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;
####
report_t
14:09:33 PDT
report_d
Fri Jun 12 2015
res
Resetting the time
report_time
00:00:00
####
Abbreviations = (rea,read,report_d,report_da,report_day,report_t,report_ti,report_tim,report_time,res,rese,reset)
Token regex = "rea|read|report_d|report_da|report_day|report_t|report_ti|report_tim|report_time|res|rese|reset"
report_t matched for report_time on line 1
report_d matched for report_day on line 3
res matched for reset on line 6
report_time matched for report_time on line 9
####
m/^report_t(?:i(?:m(?:e)?)?)?/