Newbie Count Question

George Sakkis gsakkis at rutgers.edu
Sun Oct 9 22:22:06 EDT 2005


"ProvoWallis" wrote:

> I've managed to get this far thanks to looking at other
> posts on the board but no matter waht I try all of the
> sections end up being numbered for the total number of
> sections in the document. e.g., if there are 100 sections
> in the document the "no" attribute is "1.100"
> for each one.

Of course it is; the counter you compute is fixed and equal to
len(all). What you need is a substitution function that keeps track of
the counter and increments it by one for every substitution. This means
that the counter becomes part of the function's state. When you hear
"function" and "state" together, the typical solution is "class":

import re

class ReplaceSecMain(object):
    def __init__(self):
        self._count = 0
        self._secmain_re = re.compile(r'''
            (?<= <sec-main\ no=" )   # positive lookbehind assertion
            ( \d* )                  # first number
            (?: \.\d* )              # dot and second number (ignored)
            (?= "> )                 # positive lookahead assertion
            ''', re.IGNORECASE | re.VERBOSE)

    def sub(self, text):
        return self._secmain_re.sub(self._subNum, text)

    def _subNum(self, match):
        self._count += 1
        return '%s.%.2d' % (match.group(1), self._count)


print ReplaceSecMain().sub(open("myfile.txt").read())


I also cleaned up the regular expression a little; google for
lookahead/lookbehind assertions if you haven't seen them before.

HTH,
George




More information about the Python-list mailing list