Exemple #1
0
 def search(self, string, pos=0, endpos=sys.maxint):
     """Scan through string looking for a location where this regular
     expression produces a match, and return a corresponding MatchObject
     instance. Return None if no position in the string matches the
     pattern."""
     state = _sre._State(string, pos, endpos, self.flags)
     if _sre._search(state, self._code):
         return SRE_Match(self, state)
     else:
         return None
Exemple #2
0
 def search(self, string, pos=0, endpos=sys.maxint):
     """Scan through string looking for a location where this regular
     expression produces a match, and return a corresponding MatchObject
     instance. Return None if no position in the string matches the
     pattern."""
     state = _sre._State(string, pos, endpos, self.flags)
     if _sre._search(state, self._code):
         return SRE_Match(self, state)
     else:
         return None
Exemple #3
0
    def subn(self, repl, string, count=0):
        """Return the tuple (new_string, number_of_subs_made) found by replacing
        the leftmost non-overlapping occurrences of pattern with the replacement
        repl."""
        filter = repl
        if not callable(repl) and "\\" in repl:
            # handle non-literal strings ; hand it over to the template compiler
            import re
            filter = re._subx(self, repl)
        state = _sre._State(string, 0, sys.maxint, self.flags)
        sublist = []

        need_unicode = (isinstance(string, unicode)
                        or isinstance(repl, unicode))
        n = last_pos = 0
        while not count or n < count:
            state.reset()
            if not _sre._search(state, self._code):
                break
            if last_pos < state.start:
                sublist.append(string[last_pos:state.start])
            if not (last_pos == state.start
                    and last_pos == state.string_position and n > 0):
                # the above ignores empty matches on latest position
                if callable(filter):
                    to_app = filter(SRE_Match(self, state))
                else:
                    to_app = filter
                if isinstance(to_app, unicode):
                    need_unicode = True
                sublist.append(to_app)
                last_pos = state.string_position
                n += 1
            if state.string_position == state.start:
                state.start += 1
            else:
                state.start = state.string_position

        if last_pos < state.end:
            sublist.append(string[last_pos:state.end])

        if n == 0:
            # not just an optimization -- see test_sub_unicode
            return string, n

        if need_unicode:
            item = u"".join(sublist)
        else:
            item = "".join(sublist)
        return item, n
Exemple #4
0
    def subn(self, repl, string, count=0):
        """Return the tuple (new_string, number_of_subs_made) found by replacing
        the leftmost non-overlapping occurrences of pattern with the replacement
        repl."""
        filter = repl
        if not callable(repl) and "\\" in repl:
            # handle non-literal strings ; hand it over to the template compiler
            import re
            filter = re._subx(self, repl)
        state = _sre._State(string, 0, sys.maxint, self.flags)
        sublist = []

        need_unicode = (isinstance(string, unicode) or
                        isinstance(repl, unicode))
        n = last_pos = 0
        while not count or n < count:
            state.reset()
            if not _sre._search(state, self._code):
                break
            if last_pos < state.start:
                sublist.append(string[last_pos:state.start])
            if not (last_pos == state.start and
                                last_pos == state.string_position and n > 0):
                # the above ignores empty matches on latest position
                if callable(filter):
                    to_app = filter(SRE_Match(self, state))
                else:
                    to_app = filter
                if isinstance(to_app, unicode):
                    need_unicode = True
                sublist.append(to_app)
                last_pos = state.string_position
                n += 1
            if state.string_position == state.start:
                state.start += 1
            else:
                state.start = state.string_position

        if last_pos < state.end:
            sublist.append(string[last_pos:state.end])

        if n == 0:
            # not just an optimization -- see test_sub_unicode
            return string, n

        if need_unicode:
            item = u"".join(sublist)
        else:
            item = "".join(sublist)
        return item, n
Exemple #5
0
 def findall(self, string, pos=0, endpos=sys.maxint):
     """Return a list of all non-overlapping matches of pattern in string."""
     matchlist = []
     state = _sre._State(string, pos, endpos, self.flags)
     while state.start <= state.end:
         state.reset()
         if not _sre._search(state, self._code):
             break
         match = SRE_Match(self, state)
         if self.groups == 0 or self.groups == 1:
             item = match.group(self.groups)
         else:
             item = match.groups("")
         matchlist.append(item)
         if state.string_position == state.start:
             state.start += 1
         else:
             state.start = state.string_position
     return matchlist
Exemple #6
0
 def findall(self, string, pos=0, endpos=sys.maxint):
     """Return a list of all non-overlapping matches of pattern in string."""
     matchlist = []
     state = _sre._State(string, pos, endpos, self.flags)
     while state.start <= state.end:
         state.reset()
         if not _sre._search(state, self._code):
             break
         match = SRE_Match(self, state)
         if self.groups == 0 or self.groups == 1:
             item = match.group(self.groups)
         else:
             item = match.groups("")
         matchlist.append(item)
         if state.string_position == state.start:
             state.start += 1
         else:
             state.start = state.string_position
     return matchlist        
Exemple #7
0
 def split(self, string, maxsplit=0):
     """Split string by the occurrences of pattern."""
     splitlist = []
     state = _sre._State(string, 0, sys.maxint, self.flags)
     n = 0
     last = state.start
     while not maxsplit or n < maxsplit:
         state.reset()
         if not _sre._search(state, self._code):
             break
         if state.start == state.string_position:  # zero-width match
             if last == state.end:  # or end of string
                 break
             state.start += 1
             continue
         splitlist.append(string[last:state.start])
         # add groups (if any)
         if self.groups:
             match = SRE_Match(self, state)
             splitlist.extend(list(match.groups(None)))
         n += 1
         last = state.start = state.string_position
     splitlist.append(string[last:state.end])
     return splitlist
Exemple #8
0
 def split(self, string, maxsplit=0):
     """Split string by the occurrences of pattern."""
     splitlist = []
     state = _sre._State(string, 0, sys.maxint, self.flags)
     n = 0
     last = state.start
     while not maxsplit or n < maxsplit:
         state.reset()
         if not _sre._search(state, self._code):
             break
         if state.start == state.string_position: # zero-width match
             if last == state.end:                # or end of string
                 break
             state.start += 1
             continue
         splitlist.append(string[last:state.start])
         # add groups (if any)
         if self.groups:
             match = SRE_Match(self, state)
             splitlist.extend(list(match.groups(None)))
         n += 1
         last = state.start = state.string_position
     splitlist.append(string[last:state.end])
     return splitlist