在Python,Eclipse中对正则表达式字符串发出pep8警告 [英] pep8 warning on regex string in Python, Eclipse
问题描述
为什么pep8抱怨代码中的下一个字符串?
import rere.compile("\ d {3}")
我收到的警告:
ID:W1401字符串'\ d'中的反斜杠.字符串常量可能缺少r前缀.
您能解释一下该消息的含义吗?我需要在代码中进行哪些更改,以便通过警告 W1401 ?
该代码通过了测试,并按预期运行.此外, \ d {3}
是有效的正则表达式.
"\ d"
与"\\ d"
相同,因为<代码> d .但是对于代码阅读者来说尚不清楚.
但是,请考虑 \ t
."\ t"
代表制表符,而 r"\ t"
代表文字 \
和 t
字符./p>
因此,当您指的是文字 \
和 d
时,请使用原始字符串:
re.compile(r"\ d {3}")
或显式转义反斜杠:
re.compile("\\ d {3}")
Why is pep8 complaining on the next string in the code?
import re
re.compile("\d{3}")
The warning I receive:
ID:W1401 Anomalous backslash in string: '\d'. String constant might be missing an r prefix.
Can you explain what is the meaning of the message? What do I need to change in the code so that the warning W1401 is passed?
The code passes the tests and runs as expected. Moreover \d{3}
is a valid regex.
"\d"
is same as "\\d"
because there's no escape sequence for d
. But it is not clear for the reader of the code.
But, consider \t
. "\t"
represent tab chracter, while r"\t"
represent literal \
and t
character.
So use raw string when you mean literal \
and d
:
re.compile(r"\d{3}")
or escape backslash explicitly:
re.compile("\\d{3}")
这篇关于在Python,Eclipse中对正则表达式字符串发出pep8警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!