in reply to Regex Find Words

While the above are good answers, there is an ambiguity in the spec. What result would you expect for:

my $left_text = [ qw{ when rome romans} ]; my $right_text = 'When in Rome, Romans do as the Romans do in Rome.';

Would you expect a result of 3 or of 5 (or something else)? Essentially, do you want a count of all case-insensitive matches or just a count of how many search words matched? If you want the latter, the following would suit your purposes better:

#!/usr/bin/perl use strict; use warnings; my @left_text = qw{ when rome romans}; my $right_text = 'When in Rome, Romans do as the Romans do in Rome.'; my $count = '0'; for my $search_text (@left_text) { if ($right_text =~ /\Q$search_text\E/i) { $count++ } } print "Found = $count\n"; __END__ Found = 3

Update: And while you're at it, what do you expect for

my $left_text = [ qw{ when rome romans} ]; my $right_text = 'When in the Terrordrome, do as the Romans.';

If you would only expect 2 matches (ignoring that "rome" is in Terrordrome), then you will want word boundary assertions (\b):

#!/usr/bin/perl use strict; use warnings; my @left_text = qw{ when rome romans}; my $right_text = 'When in the Terrordrome, do as the Romans.'; my $count = '0'; for my $search_text (@left_text) { if ($right_text =~ /\b\Q$search_text\E\b/i) { $count++ } } print "Found = $count\n"; __END__ Found = 2

See perlretut or perlre for more info.