emulating an and operator in regular expressions

Andrew Dalke dalke at dalkescientific.com
Mon Jan 3 17:09:17 EST 2005


Craig Ringer wrote:
> My first thought would be to express your 'A and B' regex as:
> 
> (A.*B)|(B.*A)
> 
> with whatever padding, etc, is necessary. You can even substitute in the
> sub-regex for A and B to avoid writing them out twice.

That won't work because of overlaps.  Consider

  barkeep

with a search for A='bark' and B='keep'.

Neither A.*B nor B.*A will match because the 'k' needs to
be in both A and B.

The OP asked for words, so consecutive letters separated
by non-letters or end of string.  With that restriction
this solution will work.

Another possibility is to use positive assertions, as in
  (?=A)(?=.*B)|(?=B)(?=.*A)

The best solution is to do a string.find and not worry about
implementing this as a regexp.

				Andrew
				dalke at dalkescientific.com




More information about the Python-list mailing list