I've gone through the HOWTO and the re module docs several times, and I'm still confused about how optionality and grouping interact in Python regexes. What I want is to match everything inside a group, or not at all, but I'm finding that substrings are matching. Here's a minimal example:
>> re.compile(r"(test)?").search("tes")
<_sre.SRE_MATCH at 0xBlahBlah>
I expected that not to match, since I have the entire string test marked as optional. What (part of the docs) am I not understanding??
A version of the problem that's closer to what I'm actually interested in is as follows:
>> re.compile(r"(distance|mileage)(\sbetween)?").search("distancebetween")
<_sre.SRE_MATCH at 0xBlahblah>
Why is that whitespace not being forced to match?
EDIT 2017-01-04 The answers thus far are helpful, but I think I didn't explain my need sufficiently clearly.
In short, I want a regex that will match foo or bar (in their entirety) or foo baz or bar baz (in there entirety) and nothing else.
>> m = re.compile("(foo|bar)(\sbaz)?")
>> m.search("foo ba")
<_sre.SRE_Match as 0xBlahblah>
>> m.search("foo ba").span()
(0, 3)
So I see that what's happening is that it's matching on foo and then not caring about what's further downstream. How do I get it to match only on baz or nothing at all?