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 David A. Black other posts by this author
May 8 2008 5:35PM messages near this date
Re: Doing an AND in regexp char class | Re: Doing an AND in regexp char class
Hi --

On Fri, 9 May 2008, Joel VanderWerf wrote:

>  Todd Benson wrote:
> > I'm drawing a blank here with this one.  Why doesn't this work then...
> > 
> > irb(main):006:0> r = /\A[oh]*\z/
> > => /\A[oh]*\z/
> > irb(main):007:0> s = "hello, there"
> > => "hello, there"
> > irb(main):008:0> r === s
> > => false
> 
>  Maybe I'm confused about was wanted originally. The above tests the following 
>  condition:
> 
>   (set of chars occurring in given string)
>      is_a_subset_of
>   (given set of chars).
> 
>  irb(main):007:0> /\A[oh]*\z/ === "hohoho"
>  => true
>  irb(main):008:0> /\A[oh]*\z/ === "ho ho"
>  => false
> 
>  If you want superset instead of subset, this works:
> 
>  irb(main):013:0> /(?=.*h)(?=.*o)/ === "h o"
>  => true

That depends on the order, though. To do the superset test, you could
just do the subset, but in the other direction: check that the
character class, as a string, doesn't contain anything that isn't in
the main string:

   str = "h o"
   chars = "ho"

   /\A[#{str}]*\z/ === chars    # true

(though probably best to uniquify the string first).


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS         June 9-12            Berlin
   ADVANCING WITH RAILS   June 16-19           Berlin
   INTRO TO RAILS         June 24-27           London (Skills Matter)
See http://www.rubypal.com for details and updates!
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