可以说我有:
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'''
我需要一种方法来进行替换(或只是删除),并在“ This”和“ ok”之间插入文本,以便在我调用它时等于:
a = "Example String"
我找不到任何似乎有效的通配符。任何帮助深表感谢。
您需要Regular Expression:
Regular Expression
>>> import re >>> re.sub('\nThis.*?ok','',a, flags=re.DOTALL) ' Example String'