in reply to a regex which can detect if a string have all of some characters

Here, I was playing with this.. there are bugs here..
use Test::Simple 'no_plan'; use strict; use constant DEBUG=> 1; sub debug; sub debug { print STDERR (+shift) ."\n" if DEBUG; } my $string = 'This is a great string here. Really. It even has an @ ch +ar.'; # no.. for my $chars( qw(12345 %!$*& ^ 9 x) ) # interestingly fails for ^ ok( ! string_has_chars($string => $chars ) , " we dont have '$chars +'"); } # yes.. for my $chars( qw(even has an @ char .Really) ){ ok( string_has_chars($string => $chars ) , " we do have '$chars'"); } sub string_has_chars { my $string = shift; my %char; ARG: for my $element (@_){ defined $element or next; if ( ref $element and ref $element eq 'ARRAY' ){ $element="@$element"; } for my $char ( split('',$element) ){ $char{$char}=0; } } my @chars = sort keys %char; debug("chars are [@chars]"); for my $c (@chars){ $string=~/$c/ or return; } return 1; }
On output..
leo@dali$ clear; perl /tmp/test.pl 

chars are 1 2 3 4 5
ok 1 -  we dont have '12345'
chars are ! $ % & *
ok 2 -  we dont have '%!$*&'
chars are ^
not ok 3 -  we dont have '^'
#   Failed test ' we dont have '^''
#   at /tmp/test.pl line 22.
chars are 9
ok 4 -  we dont have '9'
chars are x
ok 5 -  we dont have 'x'
chars are e n v
ok 6 -  we do have 'even'
chars are a h s
ok 7 -  we do have 'has'
chars are a n
ok 8 -  we do have 'an'
chars are @
ok 9 -  we do have '@'
chars are a c h r
ok 10 -  we do have 'char'
chars are . R a e l y
ok 11 -  we do have '.Really'
1..11
# Looks like you failed 1 test of 11.
  • Comment on Re: a regex which can detect if a string have all of some characters
  • Download Code