简体   繁体   English

如何检测字符串中的转义序列

[英]how to detect an escape sequence in a string

Given a string named line whose raw version has this value: 给定一个名为line的字符串,其原始版本具有以下值:

\rRAWSTRING

how can I detect if it has the escape character \\r? 如何检测它是否具有转义字符\\ r? What I've tried is: 我试过的是:

if repr(line).startswith('\r'):
    blah...

but it doesn't catch it. 但它没有抓住它。 I also tried find, such as: 我也试过找,如:

if repr(line).find('\r') != -1:
    blah

doesn't work either. 也不起作用。 What am I missing? 我错过了什么?

thx! 谢谢!

EDIT: 编辑:

thanks for all the replies and the corrections re terminolgy and sorry for the confusion. 感谢所有的回复和更正的终结和抱歉的混乱。

OK, if i do this 好的,如果我这样做

print repr(line)

then what it prints is: 然后打印的是:

'\rSET ENABLE ACK\n'

(including the single quotes). (包括单引号)。 i have tried all the suggestions, including: 我已经尝试了所有的建议,包括:

line.startswith(r'\r')
line.startswith('\\r') 

each of which returns False. 每个返回False。 also tried: 还尝试过:

line.find(r'\r')
line.find('\\r')

each of which returns -1 每个返回-1

If: 如果:

print repr(line)

Returns: 返回:

'\rSET ENABLE ACK\n'

Then: 然后:

line.find('\r')
line.startswith('\r')
'\r' in line

are what you are looking for. 是你在找什么。 Example: 例:

>>> line = '\rSET ENABLE ACK\n'
>>> print repr(line)
'\rSET ENABLE ACK\n'
>>> line.find('\r')
0
>>> line.startswith('\r')
True
>>> '\r' in line
True

repr() returns a display string . repr()返回一个显示字符串 It actually contains the quotes and backslashes you see when you print the line: 它实际上包含您在打印行时看到的引号和反斜杠:

>>> print line
SET ENABLE ACK

>>> print repr(line)
'\rSET ENABLE ACK\n'
>>> print len(line)
16
>>> print len(repr(line))
20

Dude, seems you have tried everything but the simplest thing, lines.startswith('\\r') : 伙计,似乎你已经尝试了除了最简单的东西lines.startswith('\\r')所有东西, lines.startswith('\\r')

>>> line = '\rSET ENABLE ACK\n'
>>> line.startswith('\r')
True
>>> '\rSET ENABLE ACK\n'.startswith('\r')
True

For now, just hold on on using repr(), \\r and r'string' and go with the simplest thing to avoid confusion. 现在,继续使用repr(),\\ r和r'string'并使用最简单的方法来避免混淆。

You can try either: 您可以尝试:

if repr(line).startswith(r'\r'):

or 要么

if line.startswith('\r'):

The latter is better: it seems like you are using repr only to get at the escaped character. 后者更好:看起来你只是使用repr来获得逃脱的角色。

It's not entirely clear what you're asking. 你问的问题并不完全清楚。 You speak of a string, but also a "raw version", and your string contains "RAWSTRING", which seems to imply you are talking about raw strings. 你说的是一个字符串,但也是一个“原始版本”,你的字符串包含“RAWSTRING”,这似乎暗示你在谈论原始字符串。

None of these are quite the same thing. 这些都不是完全相同的。

If you have an ordinary string with the character represented by '\\r' in it, then you can use any ordinary means to match: 如果你有一个普通的字符串,其中包含'\\ _'字符,那么你可以使用任何普通的方法来匹配:

>>> "\rastring".find('\r')
0
>>> 

If you defined an actual "raw string", that won't work because what you put in was not the '\\r' character, but the two characters '\\' and 'r': 如果您定义了一个实际的 “原始字符串”,那将无效,因为您输入的不是'\\ r'字符,而是两个字符'\\'和'r':

>>> r"\rastring".find('\r')
-1
>>>

In this case, or in the case of an ordinary string that happens to have the characters '\\' and 'r', and you want to find those two characters, you'll need to search using a raw string: 在这种情况下,或者在普通字符串恰好具有字符'\\'和'r'的情况下,并且您想要找到这两个字符,您需要使用原始字符串进行搜索:

>>> r"\rastring".find(r'\r')
0
>>> 

Or you can search for the sequence by escaping the backslash itself: 或者您可以通过转义反斜杠本身来搜索序列:

>>> r"\rastring".find('\\r')
0
>>> 
if '\r' in line:

If that isn't what you mean, tell us PRECISELY what is in line . 如果这不是您的意思,请尽可能地告诉我们什么是line Do this: 做这个:

print repr(line)

and copy/past the result into a edit of your question. 并将结果复制/过去编辑您的问题。

Re your subject: backslash is an escape character, "\\r" is an escaped character. 重新你的主题:反斜杠是一个转义字符,“\\ r”是一个转义字符。

Simplest: 最简单的:

>>> s = r'\rRAWSTRING'
>>> s.startswith(r'\r')
True

Unless I badly misunderstand what you're saying, you need to look for r'\\r' (or, equivalently but perhaps a tad less readably, '\\\\r' ), the escape sequence , not for '\\r' , the carriage return character itself. 除非我严重误解了你所说的内容,否则你需要寻找r'\\r' (或等效但可能有点不太可读, '\\\\r' ),转义序列而不是 '\\r' ,回车字符本身。

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

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