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

正确的Regex-Syntax for Python中的re.sub替换

发布时间:2020-12-20 11:36:55 所属栏目:Python 来源:网络整理
导读:我正在寻找正确的正则表达式来替换 Python中的这样的东西: old_string包含: some textsome text!-VLorem ipsum dolor sit amet,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam eratV-some text
我正在寻找正确的正则表达式来替换 Python中的这样的东西:

old_string包含:

some text
some text
<!-V
Lorem ipsum dolor sit amet,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat
V->
some text

我试图取代<! - V *** V->部分:

import re
new_string = re.sub(r"<!-V(.*?)V->",'',old_string)

但似乎不正确,也许任何人都可以帮助我正确的正则表达式.

解决方法

默认情况下,.与换行符不匹配.如果你想 .为了匹配这些,使用 DOTALL标志:

new_string = re.sub(r"<!-V(.*?)V->",old_string,flags=re.DOTALL)

还有替换选项.通过组合两个互补的字符类,使用匹配所有内容的字符类:

[sS]

(编辑:李大同)

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

    推荐文章
      热点阅读