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 sre filter = sre._subx(self, repl) state = _sre._State(string, 0, sys.maxint, self.flags) sublist = [] need_unicode = (isinstance(string, unicode) or isinstance(self.pattern, 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 need_unicode: item = u"".join(sublist) else: item = "".join(sublist) return item, n
def _subx(self, template, string, count=0, subn=False): filter = template if not callable(template) and "\\" in template: # handle non-literal strings ; hand it over to the template compiler import sre filter = sre._subx(self, template) # state = _State(string, 0, sys.maxint, self.flags) state = _State(string, 0, sys_maxint, self.flags) sublist = [] n = last_pos = 0 while not count or n < count: state.reset() state.string_position = state.start if not state.search(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): sublist.append(filter(SRE_Match(self, state))) else: sublist.append(filter) 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]) item = "".join(sublist) if subn: return item, n else: return item
def _subx(self, template, string, count=0, subn=False): filter = template if not callable(template) and "\\" in template: # handle non-literal strings ; hand it over to the template compiler import sre filter = sre._subx(self, template) state = _State(string, 0, sys.maxint, self.flags) sublist = [] n = last_pos = 0 while not count or n < count: state.reset() state.string_position = state.start if not state.search(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): sublist.append(filter(SRE_Match(self, state))) else: sublist.append(filter) 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]) item = "".join(sublist) if subn: return item, n else: return item
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 sre filter = sre._subx(self, repl) state = _sre._State(string, 0, sys.maxint, self.flags) sublist = [] 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): sublist.append(filter(SRE_Match(self, state))) else: sublist.append(filter) 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]) item = "".join(sublist) return item, n