in reply to Some sexy regex

I believe that you'll find Perl's built-in grep command the most useful in this situation...
#!/usr/bin/perl my $string = "Helloa, this is the string"; my @chars = qw(s,o,m,e,c,h,a,r,s); if ( grep $string =~ /$_/, @chars ) { print "OK"; } else { print "Oops"; }
grep applies the expression(s) in the first argument (either a single expression followed by a comma, or a block { expr1; expr2; .. exprN; } which is not followed by a comma) to each element of the list specified in the second argument, setting $_ respectively. comparable to a foreach loop so the above is more-or-less equivalent to
foreach (@chars) { if ($string =~ /$_/) { $anymatches++ } } if ($anymatches) { print "OK" } else { print "Oops" }
hope this helps

p.s. just showing off but you could do this on one line: print grep $string=~/$_/,@chars ? "OK" : "Oops";

Replies are listed 'Best First'.
Re: Re: Some sexy regex
by Anonymous Monk on Mar 19, 2003 at 07:39 UTC
    foreach (@chars) { if ($string=~ /$_{$anymatches++} } if($anymatches) {print "ok"} esle {print "Oops"