Python正则表达式用单反斜杠替换双反斜杠 [英] Python regex to replace double backslash with single backslash

查看:70
本文介绍了Python正则表达式用单反斜杠替换双反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在[5]中:re.sub(r'\\+', '\\', string)sre_constants.error:假转义(行尾)

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

在[6]中:re.sub(r'\\+', r'\\', string)出 [6]: 'class="highlight'

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

在[7]中:re.sub(r'\\+', r'\', string)语法错误:扫描字符串文字时 EOL

解决方案

为什么不使用 string.replace()?

<预><代码>>>>s = '一些 \\\\ 双打'>>>印刷一些\\双打>>>打印 s.replace('\\\\', '\\')一些\双打

或者使用原始"字符串:

<预><代码>>>>s = r'some \\ doubles'>>>印刷一些\\双打>>>打印 s.replace('\\\\', '\\')一些\双打

由于转义符比较复杂,你还是需要转义,这样它就不会转义'

I'm trying to replace all double backslashes with just a single backslash. I want to replace 'class=\\"highlight' with 'class=\"highlight'. I thought that python treats '\\' as one backslash and r'\\+' as a string with two backslashes. But when I try

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

So I tried switching the replace string with a raw string:

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

Which isn't what I need. So I tried only one backslash in the raw string:

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

解决方案

why not use string.replace()?

>>> s = 'some \\\\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles

Or with "raw" strings:

>>> s = r'some \\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles

Since the escape character is complicated, you still need to escape it so it does not escape the '

这篇关于Python正则表达式用单反斜杠替换双反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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