Beispiel #1
0
 def test_bug_527371(self):
     # bug described in patches 527371/672491
     self.assertEqual(re.match(r'(a)?a','a').lastindex, None)
     self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1)
     self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a')
     self.assertEqual(re.match("(?P<a>a(b))", "ab").lastgroup, 'a')
     self.assertEqual(re.match("((a))", "a").lastindex, 1)
Beispiel #2
0
    def test_getlower(self):
        import _sre
        self.assertEqual(_sre.getlower(ord('A'), 0), ord('a'))
        self.assertEqual(_sre.getlower(ord('A'), re.LOCALE), ord('a'))
        self.assertEqual(_sre.getlower(ord('A'), re.UNICODE), ord('a'))

        self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
        self.assertEqual(re.match("abc", u"ABC", re.I).group(0), "ABC")
Beispiel #3
0
 def test_bug_448951(self):
     # bug 448951 (similar to 429357, but with single char match)
     # (Also test greedy matches.)
     for op in '','?','*':
         self.assertEqual(re.match(r'((.%s):)?z'%op, 'z').groups(),
                          (None, None))
         self.assertEqual(re.match(r'((.%s):)?z'%op, 'a:z').groups(),
                          ('a:', 'a'))
Beispiel #4
0
 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.
     self.assertEqual(re.match('.*?c', 10000*'ab'+'cd').end(0), 20001)
     self.assertEqual(re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0),
                      20003)
     self.assertEqual(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.
     self.assertEqual(re.search('(a|b)*?c', 10000*'ab'+'cd').end(0), 20001)
Beispiel #5
0
 def test_search_star_plus(self):
     self.assertEqual(re.search('x*', 'axx').span(0), (0, 0))
     self.assertEqual(re.search('x*', 'axx').span(), (0, 0))
     self.assertEqual(re.search('x+', 'axx').span(0), (1, 3))
     self.assertEqual(re.search('x+', 'axx').span(), (1, 3))
     self.assertEqual(re.search('x', 'aaa'), None)
     self.assertEqual(re.match('a*', 'xxx').span(0), (0, 0))
     self.assertEqual(re.match('a*', 'xxx').span(), (0, 0))
     self.assertEqual(re.match('x*', 'xxxa').span(0), (0, 3))
     self.assertEqual(re.match('x*', 'xxxa').span(), (0, 3))
     self.assertEqual(re.match('a+', 'xxx'), None)
Beispiel #6
0
 def test_ignore_case(self):
     self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
     self.assertEqual(re.match("abc", u"ABC", re.I).group(0), "ABC")
     self.assertEqual(re.match(r"(a\s[^a])", "a b", re.I).group(1), "a b")
     self.assertEqual(re.match(r"(a\s[^a]*)", "a bb", re.I).group(1), "a bb")
     self.assertEqual(re.match(r"(a\s[abc])", "a b", re.I).group(1), "a b")
     self.assertEqual(re.match(r"(a\s[abc]*)", "a bb", re.I).group(1), "a bb")
     self.assertEqual(re.match(r"((a)\s\2)", "a a", re.I).group(1), "a a")
     self.assertEqual(re.match(r"((a)\s\2*)", "a aa", re.I).group(1), "a aa")
     self.assertEqual(re.match(r"((a)\s(abc|a))", "a a", re.I).group(1), "a a")
     self.assertEqual(re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1), "a aa")
Beispiel #7
0
    def test_bug_6561(self):
        # '\d' should match characters in Unicode category 'Nd'
        # (Number, Decimal Digit), but not those in 'Nl' (Number,
        # Letter) or 'No' (Number, Other).
        decimal_digits = [
            u'\u0037', # '\N{DIGIT SEVEN}', category 'Nd'
            u'\u0e58', # '\N{THAI DIGIT SIX}', category 'Nd'
            u'\uff10', # '\N{FULLWIDTH DIGIT ZERO}', category 'Nd'
            ]
        for x in decimal_digits:
            self.assertEqual(re.match('^\d$', x, re.UNICODE).group(0), x)

        not_decimal_digits = [
            u'\u2165', # '\N{ROMAN NUMERAL SIX}', category 'Nl'
            u'\u3039', # '\N{HANGZHOU NUMERAL TWENTY}', category 'Nl'
            u'\u2082', # '\N{SUBSCRIPT TWO}', category 'No'
            u'\u32b4', # '\N{CIRCLED NUMBER THIRTY NINE}', category 'No'
            ]
        for x in not_decimal_digits:
            self.assertIsNone(re.match('^\d$', x, re.UNICODE))
Beispiel #8
0
    def test_re_match(self):
        self.assertEqual(re.match('a', 'a').groups(), ())
        self.assertEqual(re.match('(a)', 'a').groups(), ('a',))
        self.assertEqual(re.match(r'(a)', 'a').group(0), 'a')
        self.assertEqual(re.match(r'(a)', 'a').group(1), 'a')
        self.assertEqual(re.match(r'(a)', 'a').group(1, 1), ('a', 'a'))

        pat = re.compile('((a)|(b))(c)?')
        self.assertEqual(pat.match('a').groups(), ('a', 'a', None, None))
        self.assertEqual(pat.match('b').groups(), ('b', None, 'b', None))
        self.assertEqual(pat.match('ac').groups(), ('a', 'a', None, 'c'))
        self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
        self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))

        # A single group
        m = re.match('(a)', 'a')
        self.assertEqual(m.group(0), 'a')
        self.assertEqual(m.group(0), 'a')
        self.assertEqual(m.group(1), 'a')
        self.assertEqual(m.group(1, 1), ('a', 'a'))

        pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
        self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
        self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
                         (None, 'b', None))
        self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
Beispiel #9
0
 def test_sre_character_class_literals(self):
     for i in [0, 8, 16, 32, 64, 127, 128, 255]:
         self.assertNotEqual(re.match(r"[\%03o]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\%03o0]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\%03o8]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\x%02x]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\x%02x0]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\x%02xz]" % i, chr(i)), None)
     self.assertRaises(re.error, re.match, "[\911]", "")
Beispiel #10
0
 def test_sre_character_literals(self):
     for i in [0, 8, 16, 32, 64, 127, 128, 255]:
         self.assertNotEqual(re.match(r"\%03o" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"\%03o0" % i, chr(i)+"0"), None)
         self.assertNotEqual(re.match(r"\%03o8" % i, chr(i)+"8"), None)
         self.assertNotEqual(re.match(r"\x%02x" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"\x%02x0" % i, chr(i)+"0"), None)
         self.assertNotEqual(re.match(r"\x%02xz" % i, chr(i)+"z"), None)
     self.assertRaises(re.error, re.match, "\911", "")
Beispiel #11
0
 def test_bug_725106(self):
     # capturing groups in alternatives in repeats
     self.assertEqual(re.match('^((a)|b)*', 'abc').groups(),
                      ('b', 'a'))
     self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(),
                      ('c', 'b'))
     self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(),
                      ('b', None))
     self.assertEqual(re.match('^((a)c|[ab])*', 'abc').groups(),
                      ('b', None))
     self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(),
                      ('b', 'a'))
     self.assertEqual(re.match('^(([ab])|c)*?d', 'abcd').groups(),
                      ('c', 'b'))
     self.assertEqual(re.match('^((d)|[ab])*?c', 'abc').groups(),
                      ('b', None))
     self.assertEqual(re.match('^((a)c|[ab])*?c', 'abc').groups(),
                      ('b', None))
Beispiel #12
0
 def test_re_groupref(self):
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
                      ('|', 'a'))
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1?$', 'a').groups(),
                      (None, 'a'))
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', 'a|'), None)
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a'), None)
     self.assertEqual(re.match(r'^(?:(a)|c)(\1)$', 'aa').groups(),
                      ('a', 'a'))
     self.assertEqual(re.match(r'^(?:(a)|c)(\1)?$', 'c').groups(),
                      (None, None))
Beispiel #13
0
    def test_re_groupref_exists(self):
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
                         ('(', 'a'))
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(),
                         (None, 'a'))
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'), None)
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a'), None)
        self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
                         ('a', 'b'))
        self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
                         (None, 'd'))
        self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
                         (None, 'd'))
        self.assertEqual(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))')
        self.assertEqual(p.match('abc').groups(),
                         ('a', 'b', 'c'))
        self.assertEqual(p.match('ad').groups(),
                         ('a', None, 'd'))
        self.assertEqual(p.match('abd'), None)
        self.assertEqual(p.match('ac'), None)
Beispiel #14
0
 def test_category(self):
     self.assertEqual(re.match(r"(\s)", " ").group(1), " ")
Beispiel #15
0
 def test_bug_725149(self):
     # mark_stack_base restoring before restoring marks
     self.assertEqual(re.match('(a)(?:(?=(b)*)c)*', 'abb').groups(),
                      ('a', None))
     self.assertEqual(re.match('(a)((?!(b)*))*', 'abb').groups(),
                      ('a', None, None))
Beispiel #16
0
 def test_stack_overflow(self):
     # nasty cases that used to overflow the straightforward recursive
     # implementation of repeated groups.
     self.assertEqual(re.match('(x)*', 50000*'x').group(1), 'x')
     self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
     self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
Beispiel #17
0
 def test_groupdict(self):
     self.assertEqual(re.match('(?P<first>first) (?P<second>second)',
                               'first second').groupdict(),
                      {'first':'first', 'second':'second'})
Beispiel #18
0
 def test_expand(self):
     self.assertEqual(re.match("(?P<first>first) (?P<second>second)",
                               "first second")
                               .expand(r"\2 \1 \g<second> \g<first>"),
                      "second first second first")
Beispiel #19
0
 def test_bug_113254(self):
     self.assertEqual(re.match(r'(a)|(b)', 'b').start(1), -1)
     self.assertEqual(re.match(r'(a)|(b)', 'b').end(1), -1)
     self.assertEqual(re.match(r'(a)|(b)', 'b').span(1), (-1, -1))
Beispiel #20
0
    def test_repeat_minmax(self):
        self.assertEqual(re.match("^(\w){1}$", "abc"), None)
        self.assertEqual(re.match("^(\w){1}?$", "abc"), None)
        self.assertEqual(re.match("^(\w){1,2}$", "abc"), None)
        self.assertEqual(re.match("^(\w){1,2}?$", "abc"), None)

        self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){3}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,3}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,4}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")

        self.assertEqual(re.match("^x{1}$", "xxx"), None)
        self.assertEqual(re.match("^x{1}?$", "xxx"), None)
        self.assertEqual(re.match("^x{1,2}$", "xxx"), None)
        self.assertEqual(re.match("^x{1,2}?$", "xxx"), None)

        self.assertNotEqual(re.match("^x{3}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,3}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,4}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{3}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)

        self.assertEqual(re.match("^x{}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{}$", "x{}"), None)
Beispiel #21
0
 def test_getattr(self):
     self.assertEqual(re.match("(a)", "a").pos, 0)
     self.assertEqual(re.match("(a)", "a").endpos, 1)
     self.assertEqual(re.match("(a)", "a").string, "a")
     self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1)))
     self.assertNotEqual(re.match("(a)", "a").re, None)
Beispiel #22
0
    def test_non_consuming(self):
        self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
        self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
        self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
        self.assertEqual(re.match("(a(?=\s[abc]*))", "a bc").group(1), "a")
        self.assertEqual(re.match(r"(a)(?=\s\1)", "a a").group(1), "a")
        self.assertEqual(re.match(r"(a)(?=\s\1*)", "a aa").group(1), "a")
        self.assertEqual(re.match(r"(a)(?=\s(abc|a))", "a a").group(1), "a")

        self.assertEqual(re.match(r"(a(?!\s[^a]))", "a a").group(1), "a")
        self.assertEqual(re.match(r"(a(?!\s[abc]))", "a d").group(1), "a")
        self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
        self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
Beispiel #23
0
 def test_anyall(self):
     self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0),
                      "a\nb")
     self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
                      "a\n\nb")
Beispiel #24
0
 def test_bigcharset(self):
     self.assertEqual(re.match(u"([\u2222\u2223])",
                               u"\u2222").group(1), u"\u2222")
     self.assertEqual(re.match(u"([\u2222\u2223])",
                               u"\u2222", re.UNICODE).group(1), u"\u2222")