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).