Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: string containing characters

by tybalt89 (Monsignor)
on Jan 21, 2025 at 15:17 UTC ( [id://11163789]=note: print w/replies, xml ) Need Help??


in reply to string containing characters

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163777 use warnings; my @wanted = (1, 2, 3, 4); my $pattern = join '', map "(?=.*\Q$_\E)", @wanted; for my $test ('1432', '1345', 'brown fox', '9876?54321') { printf "%20s => %s\n", $test, $test =~ /^$pattern/s ? "has all" : "does not have all"; }

Outputs:

1432 => has all 1345 => does not have all brown fox => does not have all 9876?54321 => has all

Replies are listed 'Best First'.
Re^2: string containing characters
by ikegami (Patriarch) on Jan 21, 2025 at 22:47 UTC

    Note: Possibly ok, but doesn't handle duplicates in @wanted. (Looking for two x would be done by building (?=.*x.*x).)

    The advantage of this way is that you can check a large number of strings against one list efficiently (by adding my $re = /^$pattern/s and using $re in the loop).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2025-11-17 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (72 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.