#!/usr/bin/perl -w use strict; use warnings; # Prototypes sub findMatches($$); # Input data my @keywordList = ( 'john', 'john.smith', 'john.smith@mail.com' ); # Main program my $searchText = "john's username is john.smith and his email address is john.smith\@mail.com"; my $pmatches = findMatches($searchText, \@keywordList); map { print "$_\n"; } @$pmatches; # Subroutines # # Inputs: $1 ... the text string to match against # $2 ... a pointer to the list of valid matching substrings # # Outputs: $1 ... a pointer to a list of all matches # sub findMatches($$) { my ($text, $plist) = @_; my @matches; foreach my $pattern (@$plist) { while ($text =~ /($pattern)/g) { my $result = $1; # Got a match in $1 push @matches, $result; # Save it to our master results list # Now trim off the first character (otherwise we'll be matching # against the same substring (think 'deep recursion'), and call # this subroutine again for recursively generated sub-matches. # Whatever we get (if anything) is added to the list. # $result =~ s/^.//; my $psubstr = findMatches($result, $plist); push @matches, @$psubstr; } } return \@matches; }