ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: Doing an AND in regexp char class
by Rick DeNatale other posts by this author
May 8 2008 4:34PM messages near this date
Doing an AND in regexp char class | Re: Doing an AND in regexp char class
On Thu, May 8, 2008 at 5:40 PM, Todd Benson <caduceass@[...].com>  wrote:
>  This question arises out of a couple of recent threads and may or may
>  not be a Ruby-specific question.
> 
>  I can check with a character class if one of the characters in the
>  class exists or does not exist, but can I use a regexp to check if a
>  string absolutely contains all of the characters in the class?
> 
>  Using a set perspective, I can do it like this in irb...
> 
>  s1 = "hello there"
>  s2 = "ohi"
>  (s2.unpack('c*') & s1.unpack('c*')).size == s2.size
> 
>  => false
> 
>  I use unpack to avoid creating a bunch of String objects, one for each
>  element in the array, which would happen if I used #split.  What I'm
>  wondering is if there is a way to do this with a simple regexp.

REs can do this, but may not be the best way.  The way that comes to
mind is to see if the string matches the characters in any order,
i.e. for "ohi" either ohi, oih, hio, hoi, iho, or ioh
so something like

/(o([^h]*h[^i]i|[^i]*i[^h]*h)|(h([^i]*i[^o]*o|[^o]*[^i]*i)|o([^h]*h[^o]*o|[^o]*o[^h]*h)/

meaning

   o followed by either
      zero or more non-h's folllowed by an h followed by zero or more
non-i's folllowed by an i
      or
      zero or more non-i's followed by an i followed by zero or more
non-h's followed by an h
   or
   h followed by either
      ...
  ...

I would be possible to generate such an RE from the string.

But maybe someone cleverer with REs has a better approach.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
Thread:
Todd Benson
Rick DeNatale
Ara.T.Howard
Pit Capitain
Todd Benson
Joel VanderWerf
7stud --
David A. Black
7stud --
Todd Benson
David A. Black
Joel VanderWerf
Todd Benson
David A. Black
Joel VanderWerf
David A. Black
Ara.T.Howard
Robert Dober

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved