Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: ensure ALL patterns in string

by kcott (Archbishop)
on Jan 25, 2016 at 10:39 UTC ( [id://1153552]=note: print w/replies, xml ) Need Help??


in reply to ensure ALL patterns in string

G'day previous,

Change

if (($var =~ /x/) && ($var =~ /y/) && ($var =~ /z/)) {...

to

if (grep { /x/ && /y/ && /z/ } $var) {...

My test:

#!/usr/bin/env perl -l use strict; use warnings; my @test_strings = qw{ ooo xoo oyo ooz xyo xoz oyz xyz oxo ooy zoo oxy zxo zoy zxy oox yoo ozo yox ozx yzo yzx oxyz oxoyz oxoyoz oxoyozo }; for my $var (@test_strings) { if (grep { /x/ && /y/ && /z/ } $var) { print "All in: $var"; } }

Output:

All in: xyz All in: zxy All in: yzx All in: oxyz All in: oxoyz All in: oxoyoz All in: oxoyozo

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found