How to check if a string matches all patterns in an array using smartmatch?

I want to check that a string matches multiple regex patterns. I came across a question that Brad Gilbert answered using the smartmatch operator:

my @matches = (
  qr/.*\.so$/,
  qr/.*_mdb\.v$/,
  qr/.*daidir/,
  qr/\.__solver_cache__/,
  qr/csrc/,
  qr/csrc\.vmc/,
  qr/gensimv/,
);

if( $_ ~~ @matches ){
  ...
}

An operator ifis entered if any of the patterns matches, but I want to check if all patterns match. How can i do this?

+4
source share
1 answer

The smartmatch operator does not support this. You will have to build it yourself. List :: MoreUtils ' allseems to be great to do this.

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;

It gives no way out.

$string 'fooooobarasdf', .

+2

Source: https://habr.com/ru/post/1654028/


All Articles