Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: a regex which can detect if a string have all of some characters

by mwah (Hermit)
on May 14, 2008 at 06:54 UTC ( #686473=note: print w/replies, xml ) Need Help??


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

The only non-looping/non-map solution I could come up with is the code-assertion counting technique:

... # the array positions of the chars are viewed as 'bits' # Bits: 1 1 1 1 ==> decimal 15 my @a = qw'a r z x'; use re 'eval'; our $t = 1; # $t will be modified within the regex # build the regex, essentially: /a(?{...})| b(?{...}) | c(?{...}) ... + /g my $p = 0; # position count variable during initialization my $expr = join'|', map quotemeta($_)."(?{\$t|=".(1<<$p++)."})", @a; # initialize before run $t = 0; my $mystring = 'areqrtyz'; # evaluate the regex in list context () and # look after the result in $t (comma operator =>, ) print 'no match' if () = $mystring =~ /$expr/g, $t != 15; ...

Regards

mwa

  • Comment on Re: a regex which can detect if a string have all of some characters
  • Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-04-17 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?