简体   繁体   English

您如何有条件地检查转义字符?

[英]How do you conditionally check for an escape character?

I have a string which contains the escape character \\x . 我有一个包含转义字符\\x的字符串。 However if i try to check for it with: 但是,如果我尝试使用以下方法进行检查:

if char == "\x":

I receive the error 我收到错误

ValueError: invalid \x escape

I have a string which contains the escape character \\x 我有一个包含转义符\\x的字符串

No you don't. 不,你不会。 There is no such things as "the escape character \\x ". 没有诸如“转义字符\\x ”之类的东西。 \\x is two characters, not one. \\x是两个字符,而不是一个。 The first character is the backslash, the second character is lower-case x. 第一个字符是反斜杠,第二个字符是小写x。

You have to escape the backslash in order to defeat its meaning. 您必须转义反斜杠才能击败其含义。

if two_character_string == "\\x":

or perhaps 也许

if first_char == "\\" and second_char == "x":

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM