regex 如何使用正则表达式将多行字符串拆分为一行?

kknvjkwl  于 2023-02-10  发布在  其他
关注(0)|答案(2)|浏览(177)
  • 假设我有以下字符串:
I love to eat apple and 
bananas but at the same time, I do
not like to eat oranges 
and I am not a fan of vegetables.

I also like to eat biscuits.

Meanwhile, oddly enough, I have never liked
ice creams or chocolates because I
am never a sweet-toothed.
  • 因此,我如何将多行字符串组合成一行?也就是说,我将得到以下结果:
I love to eat apple and bananas but at the same time, I do not like to eat oranges  and I am not a fan of vegetables.

I also like to eat biscuits.

Meanwhile, oddly enough, I have never liked ice creams or chocolates because I am never a sweet-toothed.

我对正则表达式还很陌生,因此我不确定如何处理这个案例。

7z5jn7bk

7z5jn7bk1#

欢迎使用re!按如下方式使用sub模块:

import re                                                                                                            
                                                                                                                     
s1 = """I love to eat apple and                                                                                      
bananas but at the same time, I do                                                                                   
not like to eat oranges                                                                                              
and I am not a fan of vegetables.                                                                                    
                                                                                                                     
I also like to eat biscuits.                                                                                         
                                                                                                                     
Meanwhile, oddly enough, I have never liked                                                                          
ice creams or chocolates because I                                                                                   
am never a sweet-toothed.                                                                                            
"""                                                                                                                  
                                                                                                                     
new_s1 = re.sub("\n(?!\n)", " ", s1)                                                                                       
                                                                                                                     
print(new_s1)
ecfdbz9o

ecfdbz9o2#

简单的str.split可能就足够了:

text = '''\
I love to eat apple and
bananas but at the same time, I do
not like to eat oranges
and I am not a fan of vegetables.

I also like to eat biscuits.

Meanwhile, oddly enough, I have never liked
ice creams or chocolates because I
am never a sweet-toothed.'''

for group in text.split('\n\n'):
    print(group.replace('\n', ' '))
    print()

图纸:

I love to eat apple and bananas but at the same time, I do not like to eat oranges and I am not a fan of vegetables.

I also like to eat biscuits.

Meanwhile, oddly enough, I have never liked ice creams or chocolates because I am never a sweet-toothed.

相关问题