4

I want to check that a string matches multiple regex patterns. I came across a related question, which 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 ){
  ...
}

The if statement is entered if any of the patterns match, but I want to check that all of the patterns match. How can I do this?

Community
  • 1
  • 1
Starfish
  • 697
  • 1
  • 6
  • 18
  • So essentially you have a list of precompiled regular expressions and you want to write code that returns true if all patterns matched the RHS of the smartmatch operator. If that's the case, please [edit] your question and include that as well as sample input/output. – simbabque Sep 08 '16 at 15:05
  • 1
    We should create a tag-synonym for _smart-matching_ and _smartmatch_. – simbabque Sep 08 '16 at 15:17
  • @simbabque If anything, smart-matching should be a synonym of smartmatch, since the [name of the operator is smartmatch](http://perldoc.perl.org/perlop.html#Smartmatch-Operator). The only users who can [propose and approve a synonym](http://stackoverflow.com/tags/smartmatch/synonyms) are ikegami, Joel Berger, raina77ow, and Kenney (and they would *all* have to vote), unless you retag some existing questions or get +5 on your answer to this question. But is the tag even necessary in the first place? I don't think we need a tag for every operator in the language. – ThisSuitIsBlackNot Sep 08 '16 at 15:56
  • @this I agree with your order. But I am of two minds about the tag. It's a feature, not just an operator. And it's hard and weird and both tags have useful wikis. I am not sure. Maybe we should discuss it on meta, though the non Perl folk might have a different opinion alltogether. – simbabque Sep 08 '16 at 16:25
  • @ThisSuitIsBlackNot, What? Why me? All for it, though. // As for whether we need the tag or not, smartmatching is definitely special enough that it might be needed. I also forsee there being more questions on the topic after smartmatch is changed (in 5.26?) – ikegami Sep 08 '16 at 17:01
  • @ikegami You need to have at least +5 score in a tag to propose/vote on synonyms. The synonym proposal system is pretty crummy though (there's still a suggestion from 2012 sitting in the queue!), so I just pinged a mod to see if they can do it. – ThisSuitIsBlackNot Sep 08 '16 at 17:05
  • @simbabque I was unable to track down a mod to make a synonym, even after [posting on Meta](http://meta.stackoverflow.com/q/334231/176646), so I just re-tagged all of the [tag:smart-matching] questions to [tag:smartmatch]. I also suggested an edit to update the [tag wiki](http://stackoverflow.com/review/suggested-edits/13763055) and [wiki excerpt](http://stackoverflow.com/review/suggested-edits/13763056), which were out of date and inaccurate. If you could take a look at my edits, I would be grateful. – ThisSuitIsBlackNot Sep 22 '16 at 18:16
  • @ThisSuitIsBlackNot nice. :) – simbabque Sep 22 '16 at 18:20

1 Answers1

2

The smartmatch operator does not support that. You'll have to build it yourself. List::MoreUtils' all seems great to do that.

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;

This gives no output.

If you change $string to 'fooooobarasdf' it will output the string.

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • 4
    It's worth noting that one shouldn't be using smartmatch anyway. It is flagged as experimental, and it *will* be removed or changed without notice. (Sounds like this will happen in 5.26.) – ikegami Sep 08 '16 at 15:34
  • @ikegami Yes. I left that out of the answer on purpose because I was planning a comment like yours, but forgot. – simbabque Sep 08 '16 at 15:40
  • 1
    [List::Util](https://metacpan.org/pod/List::Util#all) also has `all` nowadays, so you don't even need List::MoreUtils. – melpomene Sep 19 '16 at 17:58