def test_code1(self): r_code1 = get_code(r'[abc][def][ghi]') res = rsre_core.search(r_code1, "fooahedixxx") assert res is None res = rsre_core.search(r_code1, "fooahcdixxx") assert res is not None assert res.span() == (5, 8)
def main(n): assert n >= 0 pattern = [n] * n string = chr(n) * n rsre_core.search(pattern, string) # unicodestr = unichr(n) * n ctx = rsre_core.UnicodeMatchContext(pattern, unicodestr, 0, len(unicodestr), 0) rsre_core.search_context(ctx) # return 0
def run_external(t, use_search): from pypy.rlib.rsre.test.re_tests import SUCCEED, FAIL, SYNTAX_ERROR pattern, s, outcome = t[:3] if len(t) == 5: repl, expected = t[3:5] else: assert len(t) == 3 print 'trying:', t try: obj = get_code(pattern) except re.error: if outcome == SYNTAX_ERROR: return # Expected a syntax error raise if outcome == SYNTAX_ERROR: raise Exception("this should have been a syntax error") # if use_search: result = rsre_core.search(obj, s) else: # Emulate a poor man's search() with repeated match()s for i in range(len(s) + 1): result = rsre_core.match(obj, s, start=i) if result: break # if outcome == FAIL: if result is not None: raise Exception("succeeded incorrectly") elif outcome == SUCCEED: if result is None: raise Exception("failed incorrectly") # Matched, as expected, so now we compute the # result string and compare it to our expected result. start, end = result.span(0) vardict = { 'found': result.group(0), 'groups': result.group(), } #'flags': result.re.flags} for i in range(1, 100): try: gi = result.group(i) # Special hack because else the string concat fails: if gi is None: gi = "None" except IndexError: gi = "Error" vardict['g%d' % i] = gi #for i in result.re.groupindex.keys(): # try: # gi = result.group(i) # if gi is None: # gi = "None" # except IndexError: # gi = "Error" # vardict[i] = gi repl = eval(repl, vardict) if repl != expected: raise Exception("grouping error: %r should be %r" % (repl, expected))
def split(self, string, maxsplit=0): splitlist = [] start = 0 n = 0 last = 0 while not maxsplit or n < maxsplit: match = rsre_core.search(self._code, string, start, flags=self.flags) if match is None: break if match.match_start == match.match_end: # zero-width match if match.match_start == len(string): # at end of string break start = match.match_end + 1 continue splitlist.append(string[last:match.match_start]) # add groups (if any) if self.groups: match1 = self._make_match(match) splitlist.extend(match1.groups(None)) n += 1 last = start = match.match_end splitlist.append(string[last:]) return splitlist
def run_external(t, use_search): from pypy.rlib.rsre.test.re_tests import SUCCEED, FAIL, SYNTAX_ERROR pattern, s, outcome = t[:3] if len(t) == 5: repl, expected = t[3:5] else: assert len(t) == 3 print 'trying:', t try: obj = get_code(pattern) except re.error: if outcome == SYNTAX_ERROR: return # Expected a syntax error raise if outcome == SYNTAX_ERROR: raise Exception("this should have been a syntax error") # if use_search: result = rsre_core.search(obj, s) else: # Emulate a poor man's search() with repeated match()s for i in range(len(s)+1): result = rsre_core.match(obj, s, start=i) if result: break # if outcome == FAIL: if result is not None: raise Exception("succeeded incorrectly") elif outcome == SUCCEED: if result is None: raise Exception("failed incorrectly") # Matched, as expected, so now we compute the # result string and compare it to our expected result. start, end = result.span(0) vardict={'found': result.group(0), 'groups': result.group(), }#'flags': result.re.flags} for i in range(1, 100): try: gi = result.group(i) # Special hack because else the string concat fails: if gi is None: gi = "None" except IndexError: gi = "Error" vardict['g%d' % i] = gi #for i in result.re.groupindex.keys(): # try: # gi = result.group(i) # if gi is None: # gi = "None" # except IndexError: # gi = "Error" # vardict[i] = gi repl = eval(repl, vardict) if repl != expected: raise Exception("grouping error: %r should be %r" % (repl, expected))
def entrypoint2(r, string, repeat): r = array2list(r) string = hlstr(string) match = None for i in range(repeat): match = rsre_core.search(r, string) if match is None: return -1 else: return match.match_start
def search_in_file(filename): data = read(filename) p = 0 while True: res = rsre_core.search(r_code1, data, p) if res is None: break matchstart, matchstop = res.span(1) assert 0 <= matchstart <= matchstop print '%s: %s' % (filename, data[matchstart:matchstop]) p = res.span(0)[1]
def subn(self, repl, string, count=0): 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 test_empty_search(self): r_code, r = get_code_and_re(r'') for j in range(-2, 6): for i in range(-2, 6): match = r.search('abc', i, j) res = rsre_core.search(r_code, 'abc', i, j) jk = min(max(j, 0), 3) ik = min(max(i, 0), 3) if ik <= jk: assert match is not None assert match.span() == (ik, ik) assert res is not None assert res.match_start == ik and res.match_end == ik else: assert match is None assert res is None
def search(self, string, pos=0, endpos=sys.maxint): return self._make_match( rsre_core.search(self._code, string, pos, endpos, flags=self.flags))
def test_pure_literal(self): r_code3 = get_code(r'foobar') res = rsre_core.search(r_code3, "foo bar foobar baz") assert res is not None assert res.span() == (8, 14)
def test_code2(self): r_code2 = get_code(r'<item>\s*<title>(.*?)</title>') res = rsre_core.search(r_code2, "foo bar <item> <title>abc</title>def") assert res is not None assert res.span() == (8, 34)
def search(self, string, pos=0, endpos=sys.maxint): return self._make_match(rsre_core.search(self._code, string, pos, endpos, flags=self.flags))