Re: Is this a regex bug or just me?

Gregor Schmid (gs@ivu-berlin.de)
08 Feb 1995 17:47:16 GMT

>>>>> "Skip" == Skip Montanaro <skip@automatrix.com> writes:
In article <199502061926.OAA11946@dolphin.automatrix.com> Skip Montanaro <skip@automatrix.com> writes:

Skip> I have a Python script that takes a slightly higher level (and
Skip> a bit more restrictive) specification for a regular
Skip> expression, generates the corresponding regexp, then uses it
Skip> to match a bunch of lines in a file. When it finds a match,
Skip> it generates output in a different format using the various
Skip> groups that were matched.

[quite a bit deleted]

Skip> so I built the following pattern:

Skip> %{smonth}/%{sday}%{? - %{?%{emonth}/}%{eday}}

Skip> which generated

Skip> \([0-9]+\)/\([0-9]+\)\([ ]*-[ ]*\(\([0-9]+\)/\)?\([0-9]+\)\)?

Skip> and

Skip> {'smonth': 1, 'sday': 2, 'emonth': 5, 'eday': 6}

Skip> just as I expected. It works fine for date strings of the
Skip> form 1/25 or 4/30-5/1, but returns incorrect results for dates
Skip> of the form 1/25-26. The return value of group(5) is '26'
Skip> instead of None. This is especially perplexing since
Skip> group(4), which encloses group(5) correctly returns None.

I think that the counting method is different from what you describe here.
If I remember right, all regexp groups on one level are counted, then
the next level and so on.

Thus your example would be (0 (1) (2) (3 (4 (6)) 5))

So the '26' DOES belong to group 5.

I may be wrong though.

Regards,
Greg

P.S.: Did anything I just wrote make any sense :-)?