Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DeprecationWarning: invalid escape sequence - what to use instead of \d?

I've met a problem with re module in Python 3.6.5. I have this pattern in my regular expression:

'\\nRevision: (\d+)\\n'

But when I run it, I'm getting a DeprecationWarning.

I searched for the problem on SO, and haven't found the answer, actually - what should I use instead of \d+? Just [0-9]+ or maybe something else?

like image 669
mchfrnc Avatar asked Oct 05 '22 16:10

mchfrnc


People also ask

What is invalid escape sequence?

A string contains a literal character that is a reserved character in the Regex class (for example, the '(' or open parentheses character). Placing a '\' (backslash) in front of the character in the regular expression generates an 'Invalid escape sequence' compilation error.

How do you escape a character in python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do you match periods in regex?

Any character (except for the newline character) will be matched by a period in a regular expression; when you literally want a period in a regular expression you need to precede it with a backslash. Many times you'll need to express the idea of the beginning or end of a line or word in a regular expression.


1 Answers

Python 3 interprets string literals as Unicode strings, and therefore your \d is treated as an escaped Unicode character.

Declare your RegEx pattern as a raw string instead by prepending r, as below:

r'\nRevision: (\d+)\n'

This also means you can drop the escapes for \n as well since these will just be parsed as newline characters by re.

like image 352
ACascarino Avatar answered Oct 20 '22 09:10

ACascarino