in reply to Matching an array of regexes (grep in reverse)

grep { $string =~ /$_/ } @regex;

and if you don't need to count how many matches occured than you can speed up that code:

if ( grep { $string =~ /$_/ and return 1 } @regex ) { ... }

Updated: (thx to Anomalous Monk)

sub is_match { grep { $string =~ /$_/ and return 1 } @regex; } if ( is_match() ) { print qq($string matched some regex); }

Replies are listed 'Best First'.
Re^2: Matching an array of regexes (grep in reverse)
by AnomalousMonk (Archbishop) on Nov 20, 2008 at 20:58 UTC
    >perl -wMstrict -le "my @regexes = qw( foo b[a-z] ); my $string = 'fooey'; if ( grep { $string =~ /$_/ and return 1 } @regexes ) { print qq($string matched some regex); } " Can't return outside a subroutine at -e line 1. >perl -wMstrict -le "use List::MoreUtils 'any'; my @regexes = qw( foo b[a-z] ); my $string = 'fooey'; if ( any { $string =~ /$_/ } @regexes ) { print qq($string matched some regex); } " fooey matched some regex
Re^2: Matching an array of regexes (grep in reverse)
by raymor (Acolyte) on Nov 20, 2008 at 20:52 UTC
    $raymor->banghead('stupid');
    How can I do grep in reverse, I ask. By reversing the arguments in the grep EXPR, of course. I always think of the idiom grep(/regex/, @list), but the prototype is grep(BLOCK, @list) ! Thanks. I'll try to have a better question next time.
    Ray