在Python,Eclipse中对正则表达式字符串发出pep8警告 [英] pep8 warning on regex string in Python, Eclipse

查看:87
本文介绍了在Python,Eclipse中对正则表达式字符串发出pep8警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么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屋!

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