Help with regexp please

Christopher Subich majromax at gmail.com
Fri Jul 22 17:08:08 CEST 2005


Terry Hancock wrote:
> I think this is the "regexes can't count" problem.  When the repetition
> count matters, you usually need something else.  Usually some
> combination of string and list methods will do the trick, as here.

Not exactly, regexes are just fine at doing things like "first" and
"last."  The "regexes can't count" saying applies mostly to activities
that reduce to parentheses matching at arbitrary nesting.

The OP's problem could easily be written as a regex substitution, it's
just that there's no need to; I believe that the sub would be
(completely untested, and I'm probably going to use the wrong call to
re.sub anyway since I don't have the docs open):

re.sub(outline_value,'([0-9.]+)\.[0-9]+','\1')

It's just that the string.rsplit call is much more legible, much more
intutitive, doesn't do strange things if it's accidentally called on a
top-level outline value, and also extends immediately to handle
outlines of the form I.1.a.i.




More information about the Python-list mailing list