in reply to string containing characters

I would use a regex to eliminate characters from one string that appear in another. If that one string becomes empty at the end, then we know that all the characters in it occurred in the other string. So, I use this logic to decide whether the string matches all characters or not. In the following example, the "Hello World" string contains all of these characters: o r e H

What this little program does not do is that it does not count how many letters are matched. So, if you want to know if a string contains at least 5 letter A's and 3 B's and 2 C's, then this program is not going to work...

#!/usr/bin/perl -w use strict; use warnings; my $SAMPLE = "Hello World\r\n"; my $MATCHALL = "oreH"; $MATCHALL =~ s/[\Q$SAMPLE\E]+//g; if (length($MATCHALL) == 0) { print "\nCONTAINS ALL THE CHARACTERS\n"; } else { print "\nDOES NOT CONTAIN ALL THE CHARACTERS\n"; } exit;

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

    Note: Possibly ok, but doesn't handle duplicates in $MATCHALL

      Yeah, that's true. But we could turn it around and then it works:

      #!/usr/bin/perl -w use strict; use warnings; my $SAMPLE = "Hello World\r\n"; my $MATCHALL = "ooreH\r\n"; my $SAMPLE_LENGTH = length($SAMPLE); $SAMPLE =~ s/[\Q$MATCHALL\E]{1}//g; if (length($SAMPLE) == $SAMPLE_LENGTH - length($MATCHALL)) { print "\nCONTAINS ALL THE CHARACTERS\n"; } else { print "\nDOES NOT CONTAIN ALL THE CHARACTERS\n"; } exit;

        That doesn't help. Try $SAMPLE = "oxx"; $MATCHALL = "oox";.