Next / Previous / Contents / Shipman's homepage

34. Scan.upToRe(): Search for a regular expression match

logscan.py
# - - -   S c a n . u p T o R e

    def upToRe(self, r):
        '''Is there a match for regex (r) on this line?
        '''

First we use re.search() to see if there is a match for r at or after the current position on the current line. The re.search() method takes a third argument specifying the starting position.

logscan.py
        #-- 1 --
        # [ if (r is a string) and (regex (r) matches within
        #   self.line[self.pos:]) ->
        #     m  :=  a MatchObject representing that match
        #   else if compiled regex (r) matches within
        #   self.line[self.pos:]) ->
        #     m  :=  a MatchObject representing that match
        #   else -> return None ]
        if isinstance(r, basestring):
            m = re.search(r, self.line, self.pos)
        else:
            m = r.search(self.line, self.pos)
        if m is None:
            return None

Since m is now a MatchObject describing the matched portion, we can use m.start() to find the position of the start of the match.

logscan.py
        #-- 2 --
        return m.start()