raymor has asked for the wisdom of the Perl Monks concerning the following question:
There must be a shorter, simpler way. Can you think of one? The reverse, matching a single regex against an array of strings, is simply:@regex = ( 'foo' 'b[a-z]' ); $string = 'foo'; my $matched = 0; foreach $test ( @regex ) { $matched++ if ($string =~ m/$test/); } if ($matched) { ...
But here we need to do the opposite, matching one string to many regexes (to see if ANY match). I'm thinking map() may come to the rescue, but I'm not in the habit of using it, so I'm missing something. I tried:if ( grep($regex, @strings) { ...
if ( map(string =~ /$_/, @regex) ) { ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Matching an array of regexes (grep in reverse)
by ccn (Vicar) on Nov 20, 2008 at 20:07 UTC | |
by AnomalousMonk (Archbishop) on Nov 20, 2008 at 20:58 UTC | |
by raymor (Acolyte) on Nov 20, 2008 at 20:52 UTC | |
|
Re: Matching an array of regexes (grep in reverse)
by moritz (Cardinal) on Nov 20, 2008 at 20:58 UTC | |
by bingos (Vicar) on Nov 21, 2008 at 12:33 UTC | |
|
Re: Matching an array of regexes (grep in reverse)
by jeffa (Bishop) on Nov 20, 2008 at 20:12 UTC | |
|
Re: Matching an array of regexes (grep in reverse)
by gone2015 (Deacon) on Nov 20, 2008 at 20:09 UTC |