'match the given regular expression object to the current text
position.
if a match occurs, update the current text and line position.'

def match_reg(self, reg):

    mp = self.match_position
    match = reg.match(self.text, self.match_position)
    if match:
        (start, end) = match.span()
        if (end == start):
            self.match_position = (end + 1)
        else:
            self.match_position = end
        self.matched_lineno = self.lineno
        lines = re.findall('\\n', self.text[mp:self.match_position])
        cp = (mp - 1)
        while ((cp >= 0) and (cp < self.textlength) and (self.text[cp] != '\n')):
            cp -= 1
        self.matched_charpos = (mp - cp)
        self.lineno += len(lines)
    return match
