简体   繁体   中英

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.

Thank you in advance!

Here is a dot not followed by a whitespace character (including newlines):

(\.)(?!\s)

Replace it with \\1\\n .

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

Let's say we have a file text.txt with the following contents:

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

To get the needed result we use re.sub() function with specific regex pattern:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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