alandarev alandarev - 10 months ago 60
Python Question

pep8 warning on regex string in Python, Eclipse

Why is pep8 complaining on the next string in the code?

import re

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
is a valid regex.

Answer Source

"\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:


or escape backslash explicitly: