加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用python中的re.sub将“\u0026\u0026”替换为“and”.

发布时间:2020-12-20 11:44:42 所属栏目:Python 来源:网络整理
导读:我有一个包含的字符串. 我想替换所有右侧和左侧都有SPACE. 示例字符串: x x 期望的输出: x and and x ?我的代码: import reprint re.sub(r'BB','and','x x') 我的输出 x and and and x 请建议我,我该如何预防 amp;从被取代和amp ;. 解决方法 您可以使用
我有一个包含&&的字符串.
我想替换所有&&右侧和左侧都有SPACE.

示例字符串:

x&& &&&  && && x

期望的输出:

x&& &&&  and and x

?我的代码:

import re
print re.sub(r'B&&B','and','x&& &&&  && && x')

我的输出

x&& and&  and and x

请建议我,我该如何预防&&& amp;从被取代和&amp ;.

解决方法

您可以使用此环视正则表达式进行搜索:

(?<= )&&(?= )

并用和替换

码:

p = re.compile(ur'(?<= )&&(?= )',re.IGNORECASE)
test_str = u"x&& &&& && && x"

result = re.sub(p,"and",test_str)

RegEx Demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读