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

Re: [FWP] counting in REs



>>>>> "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]*$)/;



-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

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