[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] counting in REs



On September 8, 1999, Randal L. Schwartz wrote:
> >>>>> "Rich" == Rich Morin <rdm@cfcl.com> writes:
> 
> Rich> A friend of mine recently presented me with three cases 
> to match with
> Rich> a regular expression:
> 
> Rich>    1)  test for an even number of 'a's in a string
> 
> Rich>    2)  test for an odd  number of 'b's in a string
> 
> Rich>    3)  test for an even number of 'a's
> Rich>        and      an odd  number of 'b's in a string
> 
> Rich> Tasks 1 and 2 are pretty easy.  Task 3, however, is 
> rather tricky.  I
> Rich> will post some possible approaches in a followup...
> 
> Not tricky if you permit forward lookahead.
> 
> SPOILER GAP...
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> #1 = /^([^a]*a[^a]*a)[^a]*$/;
> #2 = /^([^b]*b[^b]*b)[^b]*b[^b]*$/;
> #3 = /^(?=([^a]*a[^a]*a)[^a]*$)(?=([^b]*b[^b]*b)[^b]*b[^b]*$)/;

Shouldn't that be 

#1 = /^([^a]*a[^a]*a)*[^a]*$/;
                     ^
#2 = /^([^b]*b[^b]*b)*[^b]*b[^b]*$/;
                     ^
#3 = /^(?=([^a]*a[^a]*a)*[^a]*$)(?=([^b]*b[^b]*b)*[^b]*b[^b]*$)/;
                        ^                        ^
To allow 0 or n*2 a's & 1 or (n*2)+1 b's?  

Right now, it only allows 2 a's or 3 b's, unless I'm misreading pretty
badly. 
(That's not all that unlikely:))
-- 
Mike

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe