emulating an and operator in regular expressions
Craig Ringer
craig at postnewspapers.com.au
Mon Jan 3 06:06:38 EST 2005
On Mon, 2005-01-03 at 08:52, Ross La Haye wrote:
> How can an and operator be emulated in regular expressions in Python?
> Specifically, I want to return a match on a string if and only if 2 or more
> substrings all appear in the string. For example, for a string s = 'Jones
> John' and substrings sg0 = 'Jones' and sg1 = 'John', I want to return a
> match, but for substrings sg0 = 'Jones' and sg2 = 'Frank' I do not want to
> return a match. Because the expression 'A and B' is logically equivalent to
> 'not (not A or not B)' I thought I could try something along those lines,
> but can't crack it.
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.
--
Craig Ringer
--
Craig Ringer
More information about the Python-list
mailing list