This is one of my favorite tricks,
my $re = do {
local $" = '|';
qr/@array/;
};
if ($bigstring =~ /$re/ ) {
#...
}
The customary way is to [join] '|', @array in constructing the regex.
If @array has regex metacharacters, it may be needful to escape them:
my $re = do {
local $" = '|';
qr/@{[map { quotemeta } @array]}/;
};
After Compline,
Zaxo
use strict;
use Regex::PreSuf;
my @array = qw( foo bar baz );
my $re = presuf(@array);
my $bigstring = 'whatever';
if ($bigstring =~ /$re/o) {
print "it matched";
}
For more information on this node visit: this
For the metacharacters case, how about this:
my $re = do {
local $" = '\E|\Q';
qr/\Q@array\E/;
};
That lets you ditch the map and array refernceing/dereferencing inside the regex. Though it is arguably not as clear.
----
send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.
use strict;
use Regexp::Match::Any;
my @array = qw(Foo Bar Baz);
my $bigstring = 'whatever';
if($bigstring =~ match_any(\@array)){
print "It matched\n";
}else{
print "It didn't match\n";
}
I have something like the following in one of my own modules ...
print
match_one( "string" , [ qw/character word paragraph string/ ] )
? "matched"
: "no match" ;
sub match_one
{ my ($string , $list) = @_;
foreach ( @{$list} ) { return 1 if $string =~ m/$_/; }
return;
}
An interpolated array:
use Perl6::Rules;
@cmds = ('get','put','save','load','dump','quit');
$str =~ m/ @::cmds /;
matches if any of its elements eq matches the string at that point. So the above example is equivalent to:
$str =~ /get|put|save|load|dump|quit/;
Using an joined pattern like "foo|bar|baz" can be quite inefficient depending on the patterns, since the regex engine can't find "anchored substrings" and can't guess where the match is. So here's some more or less attractive alternatives.
The first one is the one I prefer if I don't know anything about the string nor patterns since it doesn't do more matches than necessary (comparing with the other two) and need it as an expression. It utilizes an "inline sub" as I call them, I don't know if there's any other name for them. It's a closure and why I use a subroutine for this is that I want to use it as an expression and be able to jump out of it. So &{sub { ... }} is like do { ... } except you can use return in it. (Note the subtle difference between &{sub { ... }} and sub { ... }->().) I admit it is a bit ugly, but at the same time I like it.
if (&{sub { $bigstring =~ /$_/ and return 1 for @array }}) {
...;
}
Of course, a less hacky way could be
my $matched;
$matched = $bigstring =~ /$_/ and last for @array;
if ($matched) {
...;
}
which I prefer even more if I don't need it as an expression.
This next one counts the matches, which is inefficient if you just want one match.
if (grep $_, map $bigstring =~ /$_/, @array) {
...;
}
This one below does essentially the same, but is less memory hungry for big lists.
if (map $bigstring =~ /$_/ ? 1 : (), @array) {
...;
}
I'm not sure I really helped here...
ihb
perlmonks.org content © perlmonks.org and Anonymous Monk, didier, hardburn, ihb, mako132, meetraz, NodeReaper, parv, Zaxo
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03