简体   繁体   English

python用“。\\ n *”代替“。*”。 ”和“。\\ n”

[英]python replace “.* ” with “.\n*” except for “. ” and “.\n”

I have a text in a file with some lines like 我在文件中有一些行,例如

blabla.blabla

And I want have this result: 我想要这样的结果:

blabla.
blabla

ignoring the cases in which there is already the result that I want or 忽略已经有我想要的结果的情况,或

blabla. blabla

Please help me. 请帮我。 I think that I have to exploit re.sub function but I can't figure out how to handle it. 我认为我必须利用re.sub函数,但是我不知道如何处理它。

Thank you in advance! 先感谢您!

Here is a dot not followed by a whitespace character (including newlines): 这是一个点,后面没有空格字符(包括换行符):

(\.)(?!\s)

Replace it with \\1\\n . 将其替换为\\1\\n

Demo: https://regex101.com/r/qiLe2B/1 演示: https : //regex101.com/r/qiLe2B/1

Let's say we have a file text.txt with the following contents: 假设我们有一个文件text.txt ,内容如下:

blabla.blabla
blabla. blabla
one.two
three. four. five 

To get the needed result we use re.sub() function with specific regex pattern: 为了获得所需的结果,我们使用带有特定正则表达式模式的re.sub()函数:

import re

with open('test.txt', 'r') as fh:
    result = re.sub(r'(\w+\.)(?=\S)', r'\1\n', fh.read())

print(result)

The output: 输出:

blabla.
blabla
blabla. blabla
one.
two
three. four. five

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM