替换 2 个字符串 python 之间的所有文本 [英] Replace all text between 2 strings python
本文介绍了替换 2 个字符串 python 之间的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有:
a = r''' 示例这是一个很烦人的字符串占用多行和 h@s//种{s} 愚蠢的符号好的字符串'''
我需要一种在This"和ok"之间进行替换(或只是删除)和文本的方法,以便当我调用它时,a now 等于:
a = "示例字符串"
我找不到任何似乎有效的通配符.非常感谢任何帮助.
解决方案
你需要正则表达式代码>
:
Lets say I have:
a = r''' Example
This is a very annoying string
that takes up multiple lines
and h@s a// kind{s} of stupid symbols in it
ok String'''
I need a way to do a replace(or just delete) and text in between "This" and "ok" so that when I call it, a now equals:
a = "Example String"
I can't find any wildcards that seem to work. Any help is much appreciated.
解决方案
You need Regular Expression
:
>>> import re
>>> re.sub('\nThis.*?ok','',a, flags=re.DOTALL)
' Example String'
这篇关于替换 2 个字符串 python 之间的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文