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
def subn(self, repl, string, count=0): "NOT_RPYTHON" filter = repl if not callable(repl) and "\\" in repl: # handle non-literal strings; hand it over to the template compiler filter = re._subx(self, repl) start = 0 sublist = [] force_unicode = (isinstance(string, unicode) or isinstance(repl, unicode)) n = last_pos = 0 while not count or n < count: match = rsre_core.search(self._code, string, start, flags=self.flags) if match is None: break if last_pos < match.match_start: sublist.append(string[last_pos:match.match_start]) if not (last_pos == match.match_start == match.match_end and n > 0): # the above ignores empty matches on latest position if callable(filter): piece = filter(self._make_match(match)) else: piece = filter sublist.append(piece) last_pos = match.match_end n += 1 elif last_pos >= len(string): break # empty match at the end: finished # start = match.match_end if start == match.match_start: start += 1 if last_pos < len(string): sublist.append(string[last_pos:]) if n == 0: # not just an optimization -- see test_sub_unicode return string, n if force_unicode: item = u"".join(sublist) else: item = "".join(sublist) return item, n
def subn(self, repl, string, count=0): self.__check_input_type(string) n = 0 pattern = self.__tregex_compile(self.pattern) result = [] pos = 0 literal = False if not callable(repl): self.__check_input_type(repl) if isinstance(repl, str): literal = '\\' not in repl else: literal = b'\\' not in repl if not literal: import re repl = re._subx(self, repl) if not callable(repl): literal = True while (count == 0 or n < count) and pos <= len(string): match_result = tregex_call_exec(pattern.exec, string, pos) if not match_result.isMatch: break n += 1 start = match_result.getStart(0) end = match_result.getEnd(0) result.append(string[pos:start]) if literal: result.append(repl) else: _srematch = Match(self, pos, -1, match_result, string, pattern) _repl = repl(_srematch) result.append(_repl) pos = end if start == end: if pos < len(string): result.append(string[pos]) pos = pos + 1 result.append(string[pos:]) if self.__binary: return b"".join(result), n else: return "".join(result), 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 re filter = re._subx(self, template) state = _State(string, 0, sys.maxsize, 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.extend(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 update_event(self, inp=-1): self.set_output_val(0, re._subx(self.input(0), self.input(1)))