Why no RE match of A AND B?

Anders J. Munch andersjm at dancontrol.dk
Tue Mar 4 06:08:21 EST 2003


"Joshua Marshall" <jmarshal at mathworks.com> wrote:
> 
> > I seem to detect two quite different usages of "intersection":
> 
[...]
> 
> I think the difference is that in Python, the RE "abc" matches any
> string containing 'abc', not just the exact string itself.  I'd guess
> most formalists who aren't familiar with Python would write that
> regular expression as ".*abc.*", and expect they's need to write
> "(.*abc.*)&(.*xyz.*)" to match both 'abcxyz' and 'xyzabc'.

Nope, formalists like me (well in this case anyway) would say that
regular expression "abc" doesn't actively match anything by itself.
It just defines a set of strings.  'abcxyz' and 'xyzabc' are not
members of the set defined by the RE "abc".  To use it for something,
you need to use an active component such as re.match or re.search,
which do not check if the entire string and the RE match, but look for
matching _sub_strings.

I used the word "intersection" exactly to clarify that, regardless of
what the OP meant for an "&" operator to do, what I was talking about
was the "formalist" operation of intersection, which really is just
set intersection.  Please reserve "intersection" for that use, or the
confusion will be total.

- Anders






More information about the Python-list mailing list