开发者

Why does re.sub process backslashed escapes

开发者 https://www.devze.com 2023-03-13 15:43 出处:网络
The code is: import re p = r\'\\d\\d\\d\' s = \'123\' But then: re.sub(p,\'abc\\开发者_运维技巧n\',s)

The code is:

import re
p = r'\d\d\d'
s = '123'

But then:

re.sub(p,'abc\开发者_运维技巧n',s)
re.sub(p,r'abc\n',s)

gives the same result, i.e. 'abc\n'. I didn't expect the second line to have this result. I thought it should be 'abc\n'. I went to Python doc and it says:

re.sub(pattern, repl, string[, count, flags])

...repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is, \n is converted to a single newline character, \r is converted to a linefeed, and so forth...

So why does re.sub have this strange feature? I mean, shouldn't raw strings and normal strings always give different results in case of backslashed escapes?


The important part is

any backslash escapes in it are processed.

This means that whether you pass 'abc\n' or 'abc\\n' doesn't matter, as the regex engine will convert the latter to the former anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号