use strict;
use warnings;
my @array1 = qw (fil_ t_xt _erl);
my @array2 = qw (Merlin file text fils perl filled);
for my $stem (@array1) {
my $re = $stem;
$re =~ s/_/.+/;
/\b$re\b/ and print "$stem: $_\n" for @array2;
}
Output:
fil_: file
fil_: fils
fil_: filled
t_xt: text
_erl: perl
Only whole words are matched, as word boundires (\b) are used in the regex which, if omitted, will also match substrings within the dictionary words.
Hope this helps!
Update: Below is an updated version which adapts BrowserUk's preferred solution:
use strict;
use warnings;
my @array1 = qw (fil_ t_xt _erl);
my @array2 = qw (Merlin file text fils perl filled);
my $words = join ' ', @array2;
for my $stem (@array1) {
my $re = $stem;
$re =~ s/_/./;
print "$stem: $1\n" while $words =~ /\b($re)\b/g;
}
Output:
fil_: file
fil_: fils
t_xt: text
_erl: perl
If you want the "_" to be matched by more than one letter in the dictionary words, change the substitution to $re =~ s/_/\\S+/;. |