perlfaq6 - Regular Expressions ($Revision: 1.38 $, $Date: 2005/12/31
00:54:37 $)
On Wed, Aug 17, 2011 at 10:52 AM, Meir Michanie <meirg...@gmail.com> wrote:
> Hi Brian,
> The foreach loop should have the following line of code instead:
> if (/\b$pattern\b/i){print ; next LINE;}
>
> ==================================================
> How do I efficiently match many regular expressions at once?
>
> ( contributed by brian d foy )
>
> Avoid asking Perl to compile a regular expression every time
> you want to match it. In this example, perl must recompile the
> regular expression
> for every iteration of the foreach() loop since it has no way
> to know what $pattern will be.
>
> @patterns = qw( foo bar baz );
>
> LINE: while( <> )
> {
> foreach $pattern ( @patterns )
> {
> print if /\b$pattern\b/i;
> next LINE;
> }
> }
> ===================================================
>