弃用警告:无效的转义序列 - 用什么代替 \d? [英] DeprecationWarning: invalid escape sequence - what to use instead of \d?

查看:67
本文介绍了弃用警告:无效的转义序列 - 用什么代替 \d?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 3.6.5 中遇到了 re 模块的问题.我的正则表达式中有这个模式:

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

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

但是当我运行它时,我得到一个DeprecationWarning.

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

我搜索了 SO 上的问题,并且没有't 找到了答案,实际上 - 我应该使用什么来代替 \d+?只是 [0-9]+ 或者别的什么?

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?

推荐答案

Python 3 将字符串文字解释为 Unicode 字符串,因此您的 \d 被视为转义的 Unicode 字符.

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

将您的 RegEx 模式声明为原始字符串,而不是在前面加上 r,如下所示:

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

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

这也意味着您也可以删除 \n 的转义符,因为它们只会被 re 解析为换行符.

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

这篇关于弃用警告:无效的转义序列 - 用什么代替 \d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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