How do I get to *all* of the groups of an re search?

Tim Peters tim.one at comcast.net
Fri Jan 10 23:08:59 EST 2003


[Kyler Laird, discovers that a regexp group "in a loop" captures only
 the last place it matched]
> ...
> Yes, and that surprises me.  It seems so obvious that it
> should return all matched pieces and so arbitrary that it
> only returns the last one.

It would take potentially unbounded storage to remember all matches, and
would also (at least) complicate the meaning of backreferences (what is \1
supposed to match then?  "a list" of all strings ever matched by group 1?
the catentation of them?  at least one of them?  a contigous slice of the
string spanning the first and last places it matched?  etc).

> ...
> Regardless, do you find it useful?  Can you think of any time
> when you want to match a bunch of things and just end up with
> the last one?

Sure.  For example, finding the last component of a path expression, in
order to isolate the file name, is a common application.

> ...
> I'm not at all interested in how popular the solution is.

The Python developers were, though.  Regexps aren't central to Python's view
of the world, and making Python's regexps as compatible (as we could bear)
with Perl's regexps at the time was a primary design goal.

gratuitous-novelty-is-harmful-ly y'rs  - tim






More information about the Python-list mailing list