in reply to Regex Find Words

The Goatse operator, of course!

my $left_text = [ qw{ when rome romans} ]; my $right_text = 'When in Rome, do as the Romans.'; my $regex = join "|", @$left_text; my $found =()= $right_text =~ m/($regex)/ig; print "$found\n";

You're forcing the list of captures into scalar context.

Edit: Thanks for the ++, but AnomalousMonk and moritz posted the same solution earlier than I did. I just added that it had a name.

Replies are listed 'Best First'.
Re^2: Regex Find Words
by ikegami (Patriarch) on Apr 29, 2010 at 18:44 UTC

    moritz's solution is NOT the same. Your solution and Anomalous's expect regex patterns in @$left_text, but moritz's solution takes text strings and convert them into regex patterns using quotemeta.

    Furthermore, he used \b, so his solution doesn't find "when" in "whenever" like yours does.

    Mind you, if the inputs require quotemeta, \b might not work.

    Update: Added second and third paragraph.