in reply to Re^2: matching the words
in thread matching the words
Yes, that helped! Here's one option that substitutes the "_" with ".+" for use in a regex (use "." if you want only one letter to match):
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+/;.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: matching the words
by BrowserUk (Patriarch) on Feb 16, 2013 at 22:17 UTC | |
by Kenosis (Priest) on Feb 17, 2013 at 00:30 UTC | |
by BrowserUk (Patriarch) on Feb 17, 2013 at 01:59 UTC |
In Section
Seekers of Perl Wisdom