如何使用 smartmatch 检查字符串是否匹配数组中的所有模式?
How can I check that a string matches all patterns in an array using smartmatch?
我想检查一个字符串是否匹配多个正则表达式模式。我遇到了一个 related question, which Brad Gilbert answered 使用 smartmatch 运算符:
my @matches = (
qr/.*\.so$/,
qr/.*_mdb\.v$/,
qr/.*daidir/,
qr/\.__solver_cache__/,
qr/csrc/,
qr/csrc\.vmc/,
qr/gensimv/,
);
if( $_ ~~ @matches ){
...
}
如果有任何模式匹配,则输入 if
语句,但我想检查 所有 模式是否匹配。我该怎么做?
smartmatch 运算符不支持。您必须自己构建它。 List::MoreUtils' all
这样做似乎很棒。
use strict;
use warnings 'all';
use feature 'say';
use List::MoreUtils 'all';
my @matches = (
qr/foo/,
qr/ooo/,
qr/bar/,
qr/asdf/,
);
my $string = 'fooooobar';
say $string if all { $string =~ $_ } @matches;
这没有输出。
如果将 $string
更改为 'fooooobarasdf'
,它将输出字符串。
我想检查一个字符串是否匹配多个正则表达式模式。我遇到了一个 related question, which Brad Gilbert answered 使用 smartmatch 运算符:
my @matches = (
qr/.*\.so$/,
qr/.*_mdb\.v$/,
qr/.*daidir/,
qr/\.__solver_cache__/,
qr/csrc/,
qr/csrc\.vmc/,
qr/gensimv/,
);
if( $_ ~~ @matches ){
...
}
如果有任何模式匹配,则输入 if
语句,但我想检查 所有 模式是否匹配。我该怎么做?
smartmatch 运算符不支持。您必须自己构建它。 List::MoreUtils' all
这样做似乎很棒。
use strict;
use warnings 'all';
use feature 'say';
use List::MoreUtils 'all';
my @matches = (
qr/foo/,
qr/ooo/,
qr/bar/,
qr/asdf/,
);
my $string = 'fooooobar';
say $string if all { $string =~ $_ } @matches;
这没有输出。
如果将 $string
更改为 'fooooobarasdf'
,它将输出字符串。