使用Python和regex,请帮助转换
First sentence.Second sentence. Third 3.4 sentence3.Fourth sentence. Fifth5. Sixth.
First sentence. Second sentence. Third 3.4 sentence3. Fourth sentence. Fifth5. Sixth.
。,当两个字母之间有一个字符是字母时,需要插入空格。如果句点两边都是数字,则不需要插入空格。请帮助。
提前感谢。
可以使用负向前看断言来确保句点后面没有空格、$
或任何数字。您可以使用re.sub
将出现的句点替换为句点后面的空格。
>>> import re
>>> text = 'First sentence.Second sentence. Third 3.4 sentence3.Fourth sentence. Fifth5. Sixth.'
>>> re.sub('.(?!s|d|$)', '. ', text)
'First sentence. Second sentence. Third 3.4 sentence3. Fourth sentence. Fifth5. Sixth.'