def test_bug_527371(self): # bug described in patches 527371/672491 assert re.match(r'(a)?a','a').lastindex == None assert re.match(r'(a)(b)?b','ab').lastindex == 1 assert re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup == 'a' assert re.match("(?P<a>a(b))", "ab").lastgroup == 'a' assert re.match("((a))", "a").lastindex == 1
def test_getlower(self): import _sre assert _sre.getlower(ord('A'), 0) == ord('a') assert _sre.getlower(ord('A'), re.LOCALE) == ord('a') assert _sre.getlower(ord('A'), re.UNICODE) == ord('a') assert re.match("abc", "ABC", re.I).group(0) == "ABC" assert re.match("abc", u"ABC", re.I).group(0) == "ABC"
def test_bug_448951(self): # bug 448951 (similar to 429357, but with single char match) # (Also test greedy matches.) for op in '','?','*': assert re.match(r'((.%s):)?z'%op, 'z').groups() == ( (None, None)) assert re.match(r'((.%s):)?z'%op, 'a:z').groups() == ( ('a:', 'a'))
def test_re_escape(self): p="" for i in range(0, 256): p = p + chr(i) assert re.match(re.escape(chr(i)), chr(i)) is not None assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1) pat=re.compile(re.escape(p)) assert pat.match(p) is not None assert pat.match(p).span() == (0,256)
def test_bug_418626(self): # bugs 418626 at al. -- Testing Greg Chapman's addition of op code # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of # pattern '*?' on a long string. assert re.match('.*?c', 10000*'ab'+'cd').end(0) == 20001 assert re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0) == ( 20003) assert re.match('.*?cd', 20000*'abc'+'de').end(0) == 60001 # non-simple '*?' still used to hit the recursion limit, before the # non-recursive scheme was implemented. assert re.search('(a|b)*?c', 10000*'ab'+'cd').end(0) == 20001
def test_search_star_plus(self): assert re.search('x*', 'axx').span(0) == (0, 0) assert re.search('x*', 'axx').span() == (0, 0) assert re.search('x+', 'axx').span(0) == (1, 3) assert re.search('x+', 'axx').span() == (1, 3) assert re.search('x', 'aaa') == None assert re.match('a*', 'xxx').span(0) == (0, 0) assert re.match('a*', 'xxx').span() == (0, 0) assert re.match('x*', 'xxxa').span(0) == (0, 3) assert re.match('x*', 'xxxa').span() == (0, 3) assert re.match('a+', 'xxx') == None
def read_atom(reader): if IS_RPYTHON: int_re = '-?[0-9]+$' float_re = '-?[0-9][0-9.]*$' else: int_re = re.compile('-?[0-9]+$') float_re = re.compile('-?[0-9][0-9.]*$') token = reader.next() if re.match(int_re, token): return MalInt(int(token)) ## elif re.match(float_re, token): return int(token) elif token[0] == '"': end = len(token)-1 if end < 2: return MalStr(u"") else: s = unicode(token[1:end]) s = types._replace(u'\\"', u'"', s) s = types._replace(u'\\n', u"\n", s) s = types._replace(u'\\\\', u"\\", s) return MalStr(s) elif token[0] == ':': return _keywordu(unicode(token[1:])) elif token == "nil": return types.nil elif token == "true": return types.true elif token == "false": return types.false else: return MalSym(unicode(token))
def f(i): if i: s = "aaaaaa" else: s = "caaaaa" print rsre_re.match("(a|b)aa", s) print rsre_re.match("a{4}", s) print rsre_re.search("(a|b)aa", s) print rsre_re.search("a{4}", s) for x in rsre_re.findall("(a|b)a", s): print x for x in rsre_re.findall("a{2}", s): print x for x in rsre_re.finditer("(a|b)a", s): print x for x in rsre_re.finditer("a{2}", s): print x for x in rsre_re.split("(a|b)a", s): print x for x in rsre_re.split("a{2}", s): print x return 0
def test_ignore_case(self): assert re.match(r"(a\s[^a])", "a b", re.I).group(1) == "a b" assert re.match(r"(a\s[^a]*)", "a bb", re.I).group(1) == "a bb" assert re.match(r"(a\s[abc])", "a b", re.I).group(1) == "a b" assert re.match(r"(a\s[abc]*)", "a bb", re.I).group(1) == "a bb" assert re.match(r"((a)\s\2)", "a a", re.I).group(1) == "a a" assert re.match(r"((a)\s\2*)", "a aa", re.I).group(1) == "a aa" assert re.match(r"((a)\s(abc|a))", "a a", re.I).group(1) == "a a" assert re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1) == "a aa"
def test_re_match(self): assert re.match('a', 'a').groups() == () assert re.match('(a)', 'a').groups() == ('a',) assert re.match(r'(a)', 'a').group(0) == 'a' assert re.match(r'(a)', 'a').group(1) == 'a' #assert re.match(r'(a)', 'a').group(1, 1) == ('a', 'a') pat = re.compile('((a)|(b))(c)?') assert pat.match('a').groups() == ('a', 'a', None, None) assert pat.match('b').groups() == ('b', None, 'b', None) assert pat.match('ac').groups() == ('a', 'a', None, 'c') assert pat.match('bc').groups() == ('b', None, 'b', 'c') assert pat.match('bc').groups("") == ('b', "", 'b', 'c') # A single group m = re.match('(a)', 'a') assert m.group(0) == 'a' assert m.group(0) == 'a' assert m.group(1) == 'a' #assert m.group(1, 1) == ('a', 'a') pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
def test_sre_character_literals(self): for i in [0, 8, 16, 32, 64, 127, 128, 255]: assert re.match(r"\%03o" % i, chr(i)) != None assert re.match(r"\%03o0" % i, chr(i)+"0") != None assert re.match(r"\%03o8" % i, chr(i)+"8") != None assert re.match(r"\x%02x" % i, chr(i)) != None assert re.match(r"\x%02x0" % i, chr(i)+"0") != None assert re.match(r"\x%02xz" % i, chr(i)+"z") != None py.test.raises(re.error, re.match, "\911", "")
def test_sre_character_class_literals(self): for i in [0, 8, 16, 32, 64, 127, 128, 255]: assert re.match(r"[\%03o]" % i, chr(i)) != None assert re.match(r"[\%03o0]" % i, chr(i)) != None assert re.match(r"[\%03o8]" % i, chr(i)) != None assert re.match(r"[\x%02x]" % i, chr(i)) != None assert re.match(r"[\x%02x0]" % i, chr(i)) != None assert re.match(r"[\x%02xz]" % i, chr(i)) != None py.test.raises(re.error, re.match, "[\911]", "")
def test_bug_725106(self): # capturing groups in alternatives in repeats assert re.match('^((a)|b)*', 'abc').groups() == ( ('b', 'a')) assert re.match('^(([ab])|c)*', 'abc').groups() == ( ('c', 'b')) assert re.match('^((d)|[ab])*', 'abc').groups() == ( ('b', None)) assert re.match('^((a)c|[ab])*', 'abc').groups() == ( ('b', None)) assert re.match('^((a)|b)*?c', 'abc').groups() == ( ('b', 'a')) assert re.match('^(([ab])|c)*?d', 'abcd').groups() == ( ('c', 'b')) assert re.match('^((d)|[ab])*?c', 'abc').groups() == ( ('b', None)) assert re.match('^((a)c|[ab])*?c', 'abc').groups() == ( ('b', None))
def test_re_groupref(self): assert re.match(r'^(\|)?([^()]+)\1$', '|a|').groups() == ( ('|', 'a')) assert re.match(r'^(\|)?([^()]+)\1?$', 'a').groups() == ( (None, 'a')) assert re.match(r'^(\|)?([^()]+)\1$', 'a|') == None assert re.match(r'^(\|)?([^()]+)\1$', '|a') == None assert re.match(r'^(?:(a)|c)(\1)$', 'aa').groups() == ( ('a', 'a')) assert re.match(r'^(?:(a)|c)(\1)?$', 'c').groups() == ( (None, None))
def _str2num(s, radix): from rpython.rlib import rarithmetic, rfloat, rbigint from rpython.rlib.rstring import ParseStringError, ParseStringOverflowError from rpython.rlib.rsre import rsre_re as re import math try: if ((radix == 16 and re.match("^[0-9A-Fa-f]+$", s)) or (radix == 8 and re.match("^[0-7]+$", s)) or (radix == 10 and re.match("^[0-9]+$", s))): try: return values.W_Fixnum(rarithmetic.string_to_int(s, base=radix)) except ParseStringOverflowError: return values.W_Bignum(rbigint.rbigint.fromstr(s, base=radix)) if re.match("[+-]?([\d]+)?.?\d+[tT]\d", s): # it's an extflonum return values.W_ExtFlonum(s) if re.match("[+-]?([\d]+)?.?\d+[sf]\d", s): if "f" in s: f_parts = s.split("f") elif "s" in s: f_parts = s.split("s") else: raise ParseStringError("invalid floating point number : %s" % s) if len(f_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: numb = float(f_parts[0]) prec = int(f_parts[1]) p = math.pow(10, prec) except ValueError, e: return values.w_false return values.W_Flonum.make(numb*p, True) if re.match("[+-]?([\d]+)?.?\d+e\d", s): e_parts = s.split("e") if len(e_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: num = float(e_parts[0]) exp = int(e_parts[1]) p = math.pow(10, exp) except ValueError, e: return values.w_false return values.W_Flonum(num*p)
def test_re_groupref_exists(self): assert re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups() == ( ('(', 'a')) assert re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups() == ( (None, 'a')) assert re.match('^(\()?([^()]+)(?(1)\))$', 'a)') == None assert re.match('^(\()?([^()]+)(?(1)\))$', '(a') == None assert re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups() == ( ('a', 'b')) assert re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups() == ( (None, 'd')) assert re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups() == ( (None, 'd')) assert re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups() == ( ('a', '')) # Tests for bug #1177831: exercise groups other than the first group p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))') assert p.match('abc').groups() == ( ('a', 'b', 'c')) assert p.match('ad').groups() == ( ('a', None, 'd')) assert p.match('abd') == None assert p.match('ac') == None
def test_stack_overflow(self): # nasty cases that used to overflow the straightforward recursive # implementation of repeated groups. assert re.match('(x)*', 50000 * 'x').group(1) == 'x' assert re.match('(x)*y', 50000 * 'x' + 'y').group(1) == 'x' assert re.match('(x)*?y', 50000 * 'x' + 'y').group(1) == 'x'
def test_non_consuming(self): assert re.match("(a(?=\s[^a]))", "a b").group(1) == "a" assert re.match("(a(?=\s[^a]*))", "a b").group(1) == "a" assert re.match("(a(?=\s[abc]))", "a b").group(1) == "a" assert re.match("(a(?=\s[abc]*))", "a bc").group(1) == "a" assert re.match(r"(a)(?=\s\1)", "a a").group(1) == "a" assert re.match(r"(a)(?=\s\1*)", "a aa").group(1) == "a" assert re.match(r"(a)(?=\s(abc|a))", "a a").group(1) == "a" assert re.match(r"(a(?!\s[^a]))", "a a").group(1) == "a" assert re.match(r"(a(?!\s[abc]))", "a d").group(1) == "a" assert re.match(r"(a)(?!\s\1)", "a b").group(1) == "a" assert re.match(r"(a)(?!\s(abc|a))", "a b").group(1) == "a"
def test_anyall(self): assert re.match("a.b", "a\nb", re.DOTALL).group(0) == ( "a\nb") assert re.match("a.*b", "a\n\nb", re.DOTALL).group(0) == ( "a\n\nb")
def test_groupdict(self): assert re.match('(?P<first>first) (?P<second>second)', 'first second').groupdict() == ({ 'first': 'first', 'second': 'second' })
def test_repeat_minmax(self): assert re.match("^(\w){1}$", "abc") == None assert re.match("^(\w){1}?$", "abc") == None assert re.match("^(\w){1,2}$", "abc") == None assert re.match("^(\w){1,2}?$", "abc") == None assert re.match("^(\w){3}$", "abc").group(1) == "c" assert re.match("^(\w){1,3}$", "abc").group(1) == "c" assert re.match("^(\w){1,4}$", "abc").group(1) == "c" assert re.match("^(\w){3,4}?$", "abc").group(1) == "c" assert re.match("^(\w){3}?$", "abc").group(1) == "c" assert re.match("^(\w){1,3}?$", "abc").group(1) == "c" assert re.match("^(\w){1,4}?$", "abc").group(1) == "c" assert re.match("^(\w){3,4}?$", "abc").group(1) == "c" assert re.match("^x{1}$", "xxx") == None assert re.match("^x{1}?$", "xxx") == None assert re.match("^x{1,2}$", "xxx") == None assert re.match("^x{1,2}?$", "xxx") == None assert re.match("^x{3}$", "xxx") != None assert re.match("^x{1,3}$", "xxx") != None assert re.match("^x{1,4}$", "xxx") != None assert re.match("^x{3,4}?$", "xxx") != None assert re.match("^x{3}?$", "xxx") != None assert re.match("^x{1,3}?$", "xxx") != None assert re.match("^x{1,4}?$", "xxx") != None assert re.match("^x{3,4}?$", "xxx") != None assert re.match("^x{}$", "xxx") == None assert re.match("^x{}$", "x{}") != None
def test_category(self): assert re.match(r"(\s)", " ").group(1) == " "
def test_getattr(self): assert re.match("(a)", "a").pos == 0 assert re.match("(a)", "a").endpos == 1 assert re.match("(a)", "a").string == "a" assert re.match("(a)", "a").regs == ((0, 1), (0, 1)) assert re.match("(a)", "a").re != None
def test_stack_overflow(self): # nasty cases that used to overflow the straightforward recursive # implementation of repeated groups. assert re.match('(x)*', 50000*'x').group(1) == 'x' assert re.match('(x)*y', 50000*'x'+'y').group(1) == 'x' assert re.match('(x)*?y', 50000*'x'+'y').group(1) == 'x'
def test_bigcharset(self): assert re.match(u"([\u2222\u2223])", u"\u2222").group(1) == u"\u2222" assert re.match(u"([\u2222\u2223])", u"\u2222", re.UNICODE).group(1) == u"\u2222"
def test_bug_725149(self): # mark_stack_base restoring before restoring marks assert re.match('(a)(?:(?=(b)*)c)*', 'abb').groups() == (('a', None)) assert re.match('(a)((?!(b)*))*', 'abb').groups() == (('a', None, None))
def test_bug_923(self): # Issue923: grouping inside optional lookahead problem assert re.match(r'a(?=(b))?', "ab").groups() == ("b",) assert re.match(r'(a(?=(b))?)', "ab").groups() == ('a', 'b') assert re.match(r'(a)(?=(b))?', "ab").groups() == ('a', 'b') assert re.match(r'(?P<g1>a)(?=(?P<g2>b))?', "ab").groupdict() == {'g1': 'a', 'g2': 'b'}
if re.match("[+-]?([\d]+)?.?\d+e\d", s): e_parts = s.split("e") if len(e_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: num = float(e_parts[0]) exp = int(e_parts[1]) p = math.pow(10, exp) except ValueError, e: return values.w_false return values.W_Flonum(num * p) if "." in s or re.match("[+-]?([\d]+)(\.[\d]+)?e[+-][\d]+$", s): if not radix == 10: # FIXME raise SchemeException( "string->number : floats with base different than 10 are not supported yet : given number : %s - radix : %s" % (s, str(radix))) return values.W_Flonum(rfloat.string_to_float(s)) else: try: return values.W_Fixnum(rarithmetic.string_to_int(s, base=radix)) except ParseStringOverflowError: return values.W_Bignum(rbigint.rbigint.fromstr(s, base=radix)) except ParseStringError as e: return values.w_false
def test_ignore_case(self): assert re.match("abc", "ABC", re.I).group(0) == "ABC" assert re.match("abc", u"ABC", re.I).group(0) == "ABC"
def test_bug_113254(self): assert re.match(r'(a)|(b)', 'b').start(1) == -1 assert re.match(r'(a)|(b)', 'b').end(1) == -1 assert re.match(r'(a)|(b)', 'b').span(1) == (-1, -1)
def test_expand(self): assert (re.match( "(?P<first>first) (?P<second>second)", "first second").expand(r"\2 \1 \g<second> \g<first>")) == ( "second first second first")
def str2num(w_s, radix, convert_mode, decimal_mode): from rpython.rlib import rarithmetic, rfloat, rbigint from rpython.rlib.rstring import ParseStringError, ParseStringOverflowError from rpython.rlib.rsre import rsre_re as re import math s = w_s.as_str_utf8() try: if re.match("[+-]?([\d]+)?.?\d+[tT]\d", s): # it's an extflonum return values.W_ExtFlonum(s) if re.match("[+-]?([\d]+)?.?\d+[sf]\d", s): if "f" in s: f_parts = s.split("f") elif "s" in s: f_parts = s.split("s") else: raise ParseStringError("invalid floating point number : %s" % s) if len(f_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: numb = float(f_parts[0]) prec = int(f_parts[1]) p = math.pow(10, prec) except ValueError: return values.w_false return values.W_Flonum.make(numb * p, True) if re.match("[+-]?([\d]+)?.?\d+e\d", s): e_parts = s.split("e") if len(e_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: num = float(e_parts[0]) exp = int(e_parts[1]) p = math.pow(10, exp) except ValueError: return values.w_false return values.W_Flonum(num * p) if "." in s or re.match("[+-]?([\d]+)(\.[\d]+)?e[+-][\d]+$", s): if not radix.equal(values.W_Fixnum(10)): # FIXME raise SchemeException( "string->number : floats with base different than 10 are not supported yet : given number : %s - radix : %s" % (w_s.tostring(), radix.tostring())) return values.W_Flonum(rfloat.string_to_float(s)) else: try: return values.W_Fixnum( rarithmetic.string_to_int(s, base=radix.toint())) except ParseStringOverflowError: return values.W_Bignum(rbigint.rbigint.fromstr(s)) except ParseStringError as e: return values.w_false
def test_anyall(self): assert re.match("a.b", "a\nb", re.DOTALL).group(0) == ("a\nb") assert re.match("a.*b", "a\n\nb", re.DOTALL).group(0) == ("a\n\nb")
def test_bug_725149(self): # mark_stack_base restoring before restoring marks assert re.match('(a)(?:(?=(b)*)c)*', 'abb').groups() == ( ('a', None)) assert re.match('(a)((?!(b)*))*', 'abb').groups() == ( ('a', None, None))
def test_groupdict(self): assert re.match('(?P<first>first) (?P<second>second)', 'first second').groupdict() == ( {'first':'first', 'second':'second'})
def test_expand(self): assert (re.match("(?P<first>first) (?P<second>second)", "first second") .expand(r"\2 \1 \g<second> \g<first>")) == ( "second first second first")
if re.match("[+-]?([\d]+)?.?\d+e\d", s): e_parts = s.split("e") if len(e_parts) > 2: raise ParseStringError("invalid floating point number : %s" % s) try: num = float(e_parts[0]) exp = int(e_parts[1]) p = math.pow(10, exp) except ValueError, e: return values.w_false return values.W_Flonum(num*p) if "." in s or re.match("[+-]?([\d]+)(\.[\d]+)?e[+-][\d]+$", s): if not radix == 10: # FIXME raise SchemeException("string->number : floats with base different than 10 are not supported yet : given number : %s - radix : %s" % (s, str(radix))) return values.W_Flonum(rfloat.string_to_float(s)) else: try: return values.W_Fixnum(rarithmetic.string_to_int(s, base=radix)) except ParseStringOverflowError: return values.W_Bignum(rbigint.rbigint.fromstr(s, base=radix)) except ParseStringError as e: return values.w_false @expose("number->string", [values.W_Number, default(values.W_Fixnum, values.W_Fixnum.make(10))]) def num2str(a, radix): from rpython.rlib.rbigint import BASE8, BASE16