Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当句点在两个字母之间时,使用正则表达式插入一行_Python_Python 3.x - Fatal编程技术网

Python 当句点在两个字母之间时,使用正则表达式插入一行

Python 当句点在两个字母之间时,使用正则表达式插入一行,python,python-3.x,Python,Python 3.x,我试图创建一个正则表达式来解析字符串,并为夹在两个字母之间的每个句点插入两行新行 例如: string_var = 'This is my first sentence.This is my second sentence.This is my third sentence. This is my fourth sentence.This is my fifth sentence.' 除第四句外,每个句子的结尾都不在句子的最后一个单词、句点和下一句的第一个单词之间留空格。我希望输出: stri

我试图创建一个正则表达式来解析字符串,并为夹在两个字母之间的每个句点插入两行新行

例如:

string_var = 'This is my first sentence.This is my second sentence.This is my third sentence. This is my fourth sentence.This is my fifth sentence.'
除第四句外,每个句子的结尾都不在句子的最后一个单词、句点和下一句的第一个单词之间留空格。我希望输出:

string_var = 'This is my first sentence.

This is my second sentence.

This is my third sentence. This is my fourth sentence.

This is my fifth sentence.'

有人知道我是如何做到这一点的吗?

这会在句点上添加两个换行符,句点由两个字母(从技术上讲是任意字母数字字符或下划线)包围:

re.sub(r'(?<=\w)\.(?=\w)', ".\n\n", string_var)

这使用了lookarounds:它查看每个句点,并且仅当前面的字符是字母,后面的字符是字母时才匹配。这些匹配符只是外观,不会被替换文本替换。

这会在句点上添加两个换行符,句点由两个字母包围,从技术上讲,可以是任何字母数字字符或下划线:

re.sub(r'(?<=\w)\.(?=\w)', ".\n\n", string_var)

这使用了lookarounds:它查看每个句点,并且仅当前面的字符是字母,后面的字符是字母时才匹配。这些匹配器只是查看,没有被替换文本替换。

谢谢@DBedrenko。工作很有魅力:谢谢@DBedrenko。工作很有魅力: