def test_bug_449000(self): # Test for sub() on escaped characters assert re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n') == ( 'abc\ndef\n') assert re.sub('\r\n', r'\n', 'abc\r\ndef\r\n') == ( 'abc\ndef\n') assert re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n') == ( 'abc\ndef\n') assert re.sub('\r\n', '\n', 'abc\r\ndef\r\n') == ( 'abc\ndef\n')
def test_bug_1140(self): # re.sub(x, y, u'') should return u'', not '', and # re.sub(x, y, '') should return '', not u''. # Also: # re.sub(x, y, unicode(x)) should return unicode(y), and # re.sub(x, y, str(x)) should return # str(y) if isinstance(y, str) else unicode(y). for x in 'x', u'x': for y in 'y', u'y': z = re.sub(x, y, u'') assert z == u'' assert type(z) == unicode # z = re.sub(x, y, '') assert z == '' assert type(z) == str # z = re.sub(x, y, unicode(x)) assert z == y assert type(z) == unicode # z = re.sub(x, y, str(x)) assert z == y assert type(z) == type(y)
def test_sub_template_numeric_escape(self): # bug 776311 and friends assert re.sub('x', r'\0', 'x') == '\0' assert re.sub('x', r'\000', 'x') == '\000' assert re.sub('x', r'\001', 'x') == '\001' assert re.sub('x', r'\008', 'x') == '\0' + '8' assert re.sub('x', r'\009', 'x') == '\0' + '9' assert re.sub('x', r'\111', 'x') == '\111' assert re.sub('x', r'\117', 'x') == '\117' assert re.sub('x', r'\1111', 'x') == '\1111' assert re.sub('x', r'\1111', 'x') == '\111' + '1' assert re.sub('x', r'\00', 'x') == '\x00' assert re.sub('x', r'\07', 'x') == '\x07' assert re.sub('x', r'\08', 'x') == '\0' + '8' assert re.sub('x', r'\09', 'x') == '\0' + '9' assert re.sub('x', r'\0a', 'x') == '\0' + 'a' assert re.sub('x', r'\400', 'x') == '\0' assert re.sub('x', r'\777', 'x') == '\377' raises(re.error, re.sub, 'x', r'\1', 'x') raises(re.error, re.sub, 'x', r'\8', 'x') raises(re.error, re.sub, 'x', r'\9', 'x') raises(re.error, re.sub, 'x', r'\11', 'x') raises(re.error, re.sub, 'x', r'\18', 'x') raises(re.error, re.sub, 'x', r'\1a', 'x') raises(re.error, re.sub, 'x', r'\90', 'x') raises(re.error, re.sub, 'x', r'\99', 'x') raises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8' raises(re.error, re.sub, 'x', r'\11a', 'x') raises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1' raises(re.error, re.sub, 'x', r'\800', 'x') # r'\80' + '0' # in python2.3 (etc), these loop endlessly in sre_parser.py assert re.sub('(((((((((((x)))))))))))', r'\11', 'x') == 'x' assert re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz') == ( 'xz8') assert re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz') == ( 'xza')
def test_bug_449964(self): # fails for group followed by other escape assert re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx') == ( 'xx\bxx\b')
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'
def test_bug_462270(self): # Test for empty sub() behaviour, see SF bug #462270 assert re.sub('x*', '-', 'abxd') == '-a-b-d-' assert re.sub('x+', '-', 'abxd') == 'ab-d'
def test_bug_114660(self): assert re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there') == ( 'hello there')
def test_qualified_re_sub(self): assert re.sub('a', 'b', 'aaaaa') == 'bbbbb' assert re.sub('a', 'b', 'aaaaa', 1) == 'baaaa'
def test_sub_template_numeric_escape(self): # bug 776311 and friends assert re.sub('x', r'\0', 'x') == '\0' assert re.sub('x', r'\000', 'x') == '\000' assert re.sub('x', r'\001', 'x') == '\001' assert re.sub('x', r'\008', 'x') == '\0' + '8' assert re.sub('x', r'\009', 'x') == '\0' + '9' assert re.sub('x', r'\111', 'x') == '\111' assert re.sub('x', r'\117', 'x') == '\117' assert re.sub('x', r'\1111', 'x') == '\1111' assert re.sub('x', r'\1111', 'x') == '\111' + '1' assert re.sub('x', r'\00', 'x') == '\x00' assert re.sub('x', r'\07', 'x') == '\x07' assert re.sub('x', r'\08', 'x') == '\0' + '8' assert re.sub('x', r'\09', 'x') == '\0' + '9' assert re.sub('x', r'\0a', 'x') == '\0' + 'a' assert re.sub('x', r'\400', 'x') == '\0' assert re.sub('x', r'\777', 'x') == '\377' raises(re.error, re.sub, 'x', r'\1', 'x') raises(re.error, re.sub, 'x', r'\8', 'x') raises(re.error, re.sub, 'x', r'\9', 'x') raises(re.error, re.sub, 'x', r'\11', 'x') raises(re.error, re.sub, 'x', r'\18', 'x') raises(re.error, re.sub, 'x', r'\1a', 'x') raises(re.error, re.sub, 'x', r'\90', 'x') raises(re.error, re.sub, 'x', r'\99', 'x') raises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8' raises(re.error, re.sub, 'x', r'\11a', 'x') raises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1' raises(re.error, re.sub, 'x', r'\800', 'x') # r'\80' + '0' # in python2.3 (etc), these loop endlessly in sre_parser.py assert re.sub('(((((((((((x)))))))))))', r'\11', 'x') == 'x' assert re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz') == ('xz8') assert re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz') == ('xza')
def test_bug_449000(self): # Test for sub() on escaped characters assert re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n') == ('abc\ndef\n') assert re.sub('\r\n', r'\n', 'abc\r\ndef\r\n') == ('abc\ndef\n') assert re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n') == ('abc\ndef\n') assert re.sub('\r\n', '\n', 'abc\r\ndef\r\n') == ('abc\ndef\n')
def test_bug_449964(self): # fails for group followed by other escape assert re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx') == ('xx\bxx\b')
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'
def test_bug_114660(self): assert re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there') == ('hello there')