Text::Prefix::XS is a Perl module that implements something of an trie algorithm for matching (and extracting) prefixes from text strings.
A common application I had was to pre-filter lots and lots of text for a small amount of preset prefixes.
Interestingly enough, the quickest solution until I wrote this module was to use a large regular expression (as in the synopsis)
SYNOPSIS
use Text::Prefix::XS;
my @haystacks = qw(
garbage
blarrgh
FOO
meh
AA-ggrr
AB-hi!
);
my @needles = qw(AAA AB FOO FOO-BAR);
my $search = prefix_search_create( map uc($_), @needles );
my %seen_hash;
foreach my $haystack (@haystacks) {
if(my $prefix = prefix_search($search, $haystack)) {
$seen_hash{$prefix}++;
}
}
$seen_hash{'FOO'} == 1;
#Compare to:
my $re = join('|', map quotemeta $_, @needles);
$re = qr/^($re)/;
foreach my $haystack (@haystacks) {
my ($match) = ($haystack =~ $re);
if($match) {
$seen_hash{$match}++;
}
}
$seen_hash{'FOO'} == 1;
Product's homepage
Requirements:
· Perl