Python 正则表达式匹配某些字符,但不匹配开头的句点

Python 正则表达式匹配某些字符,但不匹配开头的句点,python,regex,regex-lookarounds,Python,Regex,Regex Lookarounds,我有一个字符串,其中有一些空格。我想用一个句号来代替它们,但不是已经以句号结束的句号 比如说 text = "This is the oldest European-settled town in the continental " \ "U.S.\r\nExplore the town at your leisure\r\nUpgrade to add a " \ "scenic cruise aboard \r\n" 我试图通过使用正则表达式将其更改为下面的格式 text =

我有一个字符串,其中有一些空格。我想用一个句号来代替它们,但不是已经以句号结束的句号

比如说

text = "This is the oldest European-settled town in the continental " \
   "U.S.\r\nExplore the town at your leisure\r\nUpgrade to add a " \
   "scenic cruise aboard \r\n"
我试图通过使用正则表达式将其更改为下面的格式

text = "This is the oldest European-settled town in the continental " \
   "U.S. Explore the town at your leisure. Upgrade to add" \
   " a scenic cruise aboard."
我现在得到的是:

new_text = re.sub("(( )?(\\n|\\r\\n)+)", ". ", text).strip()
但是,它不考虑以句号结尾的句子。我应该在这里使用一些环顾区吗?如何使用


提前谢谢

如果你只是想摆脱新台词,就用这个

text = "This is the oldest European-settled town in the continental U.S.\r\nExplore the town at your leisure\r\nUpgrade to add a scenic cruise aboard \r\n"
text = text.replace('\r\n','')

您可以在regexp:
(()?\.?(\\n | \\r\\n)+
中添加“.”。如果有一个“.”它也将被替换为“.”

好吧,我不确定你的意思是
\r\n
是不是字面意思,所以

文字:

>>> import re
>>> text = r"This is the oldest European-settled town in the continental U.S.\r\nExplore the town at your leisure\r\nUpgrade to add a scenic cruise aboard \r\n"
>>> result = re.sub(r'[ .]*(?:(?:\\r)?\\n)+', '. ', text).strip()
>>> print(result)
This is the oldest European-settled town in the continental U.S. Explore the town at your leisure. Upgrade to add a scenic cruise aboard.

非文字:

>>> import re
>>> text = "This is the oldest European-settled town in the continental U.S.\r\nExplore the town at your leisure\r\nUpgrade to add a scenic cruise aboard \r\n"
>>> result = re.sub(r'[ .]*(?:\r?\n)+', '. ', text).strip()
>>> print(result)
This is the oldest European-settled town in the continental U.S. Explore the town at your leisure. Upgrade to add a scenic cruise aboard.

我删除了一些不必要的组,并将一些其他组转换为非捕获组


我还将
(\\n | \\r\\n)+
转换为一种性能稍高的形式
(?:(?:\\r)?\\n)+

我认为“\r\n”所以我们只需要text=text。替换('\\r\\n',”)@python知道如何摆脱这些。我试过上面的例子,它是有效的。