【发布时间】:2022-01-15 09:49:30
【问题描述】:
我正在尝试删除文本文件中每行末尾的两个换行符,但前提是它遵循小写字母,即 [a-z]。 我有以下代码:
import re
text = "I want this \n\nwith one line break \n\nIn this place \nNothing happens \n\nHere\nnothing should\nhappen too."
texts = re.sub(r"(?<=[a-z])\r?\n\n"," ", text)
print(texts)
我要获取的文字
text = "I want this \nwith one line break \n\nIn this place \nNothing happens \n\nHere\nnothing should\nhappen too."
在我的代码中,文本没有任何变化。我的代码遵循这个已回答的问题:Regular expression to remove line breaks,但我无法使其适应两个换行符。
【问题讨论】:
-
“如果它跟在小写字母后面”是指它跟在小写字母后面吗?恰恰相反