添加反斜杠而不转义 [英] Adding backslashes without escaping

查看:43
本文介绍了添加反斜杠而不转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转义字符串中的 &(与号)字符.问题是每当我 string = string.replace ('&', '\&') 结果是 '\\&'.添加了额外的反斜杠以转义原始反斜杠.如何删除这个额外的反斜杠?

解决方案

结果 '\\&' 只显示 - 实际上字符串是 \&:

<预><代码>>>>str = '&'>>>new_str = str.replace('&', '\&')>>>新字符串'\\&'>>>打印 new_str\&

在 shell 中尝试一下.

I need to escape a & (ampersand) character in a string. The problem is whenever I string = string.replace ('&', '\&') the result is '\\&'. An extra backslash is added to escape the original backslash. How do I remove this extra backslash?

解决方案

The result '\\&' is only displayed - actually the string is \&:

>>> str = '&'
>>> new_str = str.replace('&', '\&')
>>> new_str
'\\&'
>>> print new_str
\&

Try it in a shell.

这篇关于添加反斜杠而不转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆