我正在尝试制作一个正则表达式来解析一个字符串,并为夹在两个字母之间的每个句点插入两个新行。
例如:
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)
这利用了环顾四周:它查看每个句点,并且仅当它前面的字符是字母且后面的字符是字母时才匹配。这些匹配器只是看起来,并没有被替换文本替换。
关于python - 当句号介于两个字母之间时,使用正则表达式插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49035304/