如何使用smartmatch检查字符串是否与数组中的所有模式匹配?

时间:2016-09-08 14:59:22

标签: regex perl smartmatch

我想检查字符串是否匹配多个正则表达式模式。我遇到了related question,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语句,但我想检查模式的所有是否匹配。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

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',则会输出字符串。

相关问题