Logo Search packages:      
Sourcecode: aap version File versions

def aap-1.090::aapre::RegexObject::subn (   self,
  repl,
  source,
  count = 0 
)

subn(repl, string[, count=0]) -> tuple

Perform the same operation as sub(), but return a tuple
(new_string, number_of_subs_made).

Definition at line 357 of file aapre.py.

00357                                          :
        """subn(repl, string[, count=0]) -> tuple

        Perform the same operation as sub(), but return a tuple
        (new_string, number_of_subs_made).

        """
        if count < 0:
            raise error, "negative substitution count"
        if count == 0:
            count = sys.maxint
        n = 0           # Number of matches
        pos = 0         # Where to start searching
        lastmatch = -1  # End of last match
        results = []    # Substrings making up the result
        end = len(source)

        if type(repl) is type(''):
            # See if repl contains group references (if it does,
            # pcre_expand will attempt to call _Dummy.group, which
            # results in a TypeError)
            try:
                repl = pcre_expand(_Dummy, repl)
            except (error, TypeError):
                m = MatchObject(self, source, 0, end, [])
                repl = lambda m, repl=repl, expand=pcre_expand: expand(m, repl)
            else:
                m = None
        else:
            m = MatchObject(self, source, 0, end, [])

        match = self.code.match
        append = results.append
        while n < count and pos <= end:
            regs = match(source, pos, end, 0)
            if not regs:
                break
            self._num_regs = len(regs)
            i, j = regs[0]
            if i == j == lastmatch:
                # Empty match adjacent to previous match
                pos = pos + 1
                append(source[lastmatch:pos])
                continue
            if pos < i:
                append(source[pos:i])
            if m:
                m.pos = pos
                m.regs = regs
                append(repl(m))
            else:
                append(repl)
            pos = lastmatch = j
            if i == j:
                # Last match was empty; don't try here again
                pos = pos + 1
                append(source[lastmatch:pos])
            n = n + 1
        append(source[pos:])
        return (''.join(results), n)

    def split(self, source, maxsplit=0):


Generated by  Doxygen 1.6.0   Back to index