正则表达式替换中的Python非转义字符串

2024-10-16 20:47:08 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码的输出:

rpl = 'This is a nicely escaped newline \\n'
my_string = 'I hope this apple is replaced with a nicely escaped string'
reg = re.compile('apple')
reg.sub( rpl, my_string )

…是:

^{pr2}$

…所以打印时:

I hope this This is a nicely escaped newline

is replaced with a nicely escaped string

那么python在替换另一个字符串中的“apple”时,它是在回避这个字符串吗?现在我刚做完

reg.sub( rpl.replace('\\','\\\\'), my_string )

这个安全吗?有没有办法阻止Python这么做?在


Tags: 字符串applestringismywithnewlinereg
2条回答

来自help(re.sub)[emphasis mine]:

sub(pattern, repl, string, count=0, flags=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.

解决此问题的一种方法是传递lambda

>>> reg.sub(rpl, my_string )
'I hope this This is a nicely escaped newline \n is replaced with a nicely escaped string'
>>> reg.sub(lambda x: rpl, my_string )
'I hope this This is a nicely escaped newline \\n is replaced with a nicely escaped string'

Python的re模块使用的所有regex模式都是非转义的,包括搜索和替换模式。这就是为什么在Python中,r修饰符通常与regex模式一起使用,因为它减少了编写可用模式所需的“回击”量。在

r修饰符出现在字符串常量之前,基本上使所有\字符(字符串分隔符之前的字符除外)一字不差。所以,r'\\' == '\\\\',和{}。在

把你的例子写成

rpl = r'This is a nicely escaped newline \\n'
my_string = 'I hope this apple is replaced with a nicely escaped string'
reg = re.compile(r'apple')
reg.sub( rpl, my_string )

按预期工作。在

相关问题 更多 >