开发者

Why does re.sub process backslashed escapes

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜