in reply to How to define a list of regular expressions?

And, to avoid looping through all the regexes, it's possible to combine the individual regexes into a single regex (probably a big performance win if there are many regexes):
>perl -wMstrict -le "my @regexes = ( qr{ foo }xms, qr{ bar }xms, qr{ baz }xms, ); my $any_regex = qr{ @{[ join '|', @regexes ]} }xms; for my $file (@ARGV) { print $file, $file =~ $any_regex ? ' MATCHES' : ' no match'; } " xfooy snork bar xfooy MATCHES snork no match bar MATCHES
Remember, however, that Perl's  | (vertical bar) regex alternation operator provides ordered alternation: the overall alternation will match with whatever sub-pattern first matches in the alternation sequence, regardless of length. See Matching this or that in perlretut.

Replies are listed 'Best First'.
Re^2: How to define a list of regular expressions?
by poolpi (Hermit) on Jul 02, 2009 at 07:32 UTC

    See Regexp::Assemble

    #!/usr/bin/perl use strict; use warnings; use Regexp::Assemble; my $ra = Regexp::Assemble->new->add(qw[foo bar baz]); print $ra->re, "\n"; for (qw[xfooy snork bar]) { print " $_ does " . ( /$ra/ ? '' : 'not ' ) . "match\n"; } __END__ Output: (?-xism:(?:ba[rz]|foo)) xfooy does match snork does not match bar does match


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb