Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    def test_basic_re_sub(self):
        assert re.sub("(?i)b+", "x", "bbbb BBBB") == 'x x'
        assert re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y') == (
                         '9.3 -3 24x100y')
        assert re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3) == (
                         '9.3 -3 23x99y')

        assert re.sub('.', lambda m: r"\n", 'x') == '\\n'
        assert re.sub('.', r"\n", 'x') == '\n'

        s = r"\1\1"
        assert re.sub('(.)', s, 'x') == 'xx'
        assert re.sub('(.)', re.escape(s), 'x') == s
        assert re.sub('(.)', lambda m: s, 'x') == s

        assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
        assert re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
        assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
        assert re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'

        assert re.sub('a',r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D','a') == (
                         '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
        assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
        assert re.sub('a', '\t\n\v\r\f\a', 'a') == (
                         (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))

        assert re.sub('^\s*', 'X', 'test') == 'Xtest'
Exemplo n.º 4
0
    def test_basic_re_sub(self):
        assert re.sub("(?i)b+", "x", "bbbb BBBB") == 'x x'
        assert re.sub(r'\d+', self.bump_num,
                      '08.2 -2 23x99y') == ('9.3 -3 24x100y')
        assert re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y',
                      3) == ('9.3 -3 23x99y')

        assert re.sub('.', lambda m: r"\n", 'x') == '\\n'
        assert re.sub('.', r"\n", 'x') == '\n'

        s = r"\1\1"
        assert re.sub('(.)', s, 'x') == 'xx'
        assert re.sub('(.)', re.escape(s), 'x') == s
        assert re.sub('(.)', lambda m: s, 'x') == s

        assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
        assert re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
        assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
        assert re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'

        assert re.sub('a', r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D',
                      'a') == ('\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
        assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
        assert re.sub('a', '\t\n\v\r\f\a',
                      'a') == ((chr(9) + chr(10) + chr(11) + chr(13) +
                                chr(12) + chr(7)))

        assert re.sub('^\s*', 'X', 'test') == 'Xtest'
Exemplo n.º 5
0
 def test_bug_612074(self):
     pat=u"["+re.escape(u"\u2039")+u"]"
     assert re.compile(pat) and 1 == 1
Exemplo n.º 6
0
 def test_bug_612074(self):
     pat = u"[" + re.escape(u"\u2039") + u"]"
     assert re.compile(pat) and 1 == 1