我有以下意见:
input = "I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"首先,每句话都应该移到一个新的行。然后,除"/“、”‘“、"-”、"+“和"$”外,所有标点符号都应与单词分开。
因此,产出应该是:
"I love programming with Python-3 . 3 !
Do you ?
It's great . . .
I give it a 10/10 .
It's free-to-use , no $$$ involved !"我使用了以下代码:
>>> import re
>>> re.sub(r"([\w/'+$\s-]+|[^\w/'+$\s-]+)\s*", r"\1 ", input)
"I love programming with Python-3 . 3 ! Do you ? It's great ... I give it a 10/10 . It's free- to-use , no $$$ involved ! "但问题是它并没有把句子分成新的行。在创建标点符号和字符之间的空白之前,我如何使用正则表达式来实现这一点?
发布于 2015-01-07 06:55:14
([!?.])(?=\s*[A-Z])\s*您可以使用此正则表达式在regex.See demo.Replace by \1\n之前创建句子。
https://regex101.com/r/sH8aR8/5
x="I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"
print re.sub(r"([!?.])(?=\s*[A-Z])",r"\1\n",x)编辑:
(?<![A-Z][a-z])([!?.])(?=\s*[A-Z])\s*尝试不同数据集的this.See演示。
发布于 2015-01-07 06:50:55
有点像
>>> import re
>>> from string import punctuation
>>> print re.sub(r'(?<=['+punctuation+'])\s+(?=[A-Z])', '\n', input)
I love programming with Python-3.3!
Do you?
It's great...
I give it a 10/10.
It's free-to-use, no $$$ involved!https://stackoverflow.com/questions/27813744
复制相似问题