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_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_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_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_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' }
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_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_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 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_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_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_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 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)?') assert pat.match('a').group(1, 2, 3) == ('a', None, None) assert pat.match('b').group('a1', 'b2', 'c3') == ((None, 'b', None)) assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
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)?') assert pat.match('a').group(1, 2, 3) == ('a', None, None) assert pat.match('b').group('a1', 'b2', 'c3') == ( (None, 'b', None)) assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
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 raises(re.error, re.match, "[\911]", "")
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 raises(re.error, re.match, "\911", "")
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 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 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_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_category(self): assert re.match(r"(\s)", " ").group(1) == " "
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_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_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_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_groupdict(self): assert re.match('(?P<first>first) (?P<second>second)', 'first second').groupdict() == ({ 'first': 'first', 'second': 'second' })
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_groupdict(self): assert re.match('(?P<first>first) (?P<second>second)', 'first second').groupdict() == ( {'first':'first', 'second':'second'})
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_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_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_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_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 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_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 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_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'}