首页 > 解决方案 > 如何删除每行特殊字符串之后的字符串

问题描述

我尝试使用 python 将文本转换为 new_text。我无法在以下句子中的 'metamorphemes=' 之后匹配。我怎样才能做到这一点?

text = "I have no idea about metamorphemes=sentence. I have idea about metamorphemes=python."

输出

new_text = "I have no idea about. I have idea about."

标签: python

解决方案


使用re.sub,我们可以从每个句子中删除 key=value 对:

text = "I have no idea about metamorphemes=sentence. I have idea about metamorphemes=python."
output = re.sub(r'\s*\w+=\w+', '', text)
print(output)

这打印:

I have no idea about. I have idea about.

推荐阅读