Next / Previous / Contents / Shipman's homepage

41. Scan.reMatch(): Match a regular expression

logscan.py
# - - -   S c a n . r e M a t c h

    def reMatch(self, r):
        '''See if the text at self.pos matches a regular expression.
        '''
        #-- 1 --
        # [ if (r is a string) and (regex (r) matches self.line
        #   starting at self.pos ->
        #       m  :=  a MatchObject representing that match
        #   else if compiled regex (r) matches self.line starting
        #   at self.pos ->
        #       m  :=  a MatchObject representing that match
        #   else ->
        #       m  :=  None ]

There is an odd asymmetry in the re package: the .match() method on a compiled RE has optional arguments to specify starting and ending locations in the source string, but the re.match() function does not. My first try at the second line below was “m = re.match(r, self.line, self.pos)”, which fails.

logscan.py
        if isinstance(r, basestring):
            m = re.match(r, self.line[self.pos:])
        else:
            m = r.match(self.line, self.pos)

        #-- 2 --
        return m