相关疑难解决方法(0)

DeprecationWarning:无效的转义序列 - 使用什么而不是\ d?

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

'\\nRevision: (\d+)\\n'
Run Code Online (Sandbox Code Playgroud)

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

在SO上搜索了这个问题,但实际上还没有找到答案 - 我应该用什么而不是\d+?只是[0-9]+或者别的什么?

python regex python-3.x

56
推荐指数
2
解决办法
3万
查看次数

Python正则表达式用单反斜杠替换双反斜杠

我试图用一个反斜杠替换所有双反斜杠.我想用'class = \"highlight'替换'class = \\'highlight'.我认为python将'\\'视为一个反斜杠,将r'\\ +'视为带有两个反斜杠的字符串(这些是带有SO转义的3和4个反斜杠).但是,当我尝试

In [5]: re.sub(r'\\+', '\\', string)
sre_constants.error: bogus escape (end of line)
Run Code Online (Sandbox Code Playgroud)

所以我尝试用原始字符串切换替换字符串:

In [6]: re.sub(r'\\+', r'\\', string)
Out [6]: 'class=\\"highlight'
Run Code Online (Sandbox Code Playgroud)

这不是我需要的.所以我在原始字符串中只尝试了一个反斜杠:

In [7]: re.sub(r'\\+', r'\', string)
SyntaxError: EOL while scanning string literal    
Run Code Online (Sandbox Code Playgroud)

python regex

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

regex ×2

python-3.x ×1