Home > Net >  How to replace repeated new lines with one removing single new lines?
How to replace repeated new lines with one removing single new lines?

Time:04-18

I am new to python. I am trying to clean texts Looking for a function that will do

  1. regex_replace('\n{2,}', '\n\n', text);
  2. regex_replace('\n', '', text);
sample = "he\nll\no\n\n\n\n My nam\ne is je\nff\n\nNew to Python";
#expected_output = "hello\n\nMy name is jeff\n\nNew to Python'

php similar problem

CodePudding user response:

You may use this regex with alternation and a capture group:

(\n\n)\n*|\n

Replacement would be \1 to make sure we get \n\n for the case when there are more than 2 line breaks and an empty string when there is just \n.

RegEx Demo

Code:

import re

s = "he\nll\no\n\n\n\n My nam\ne is je\nff\n\nNew to Python"
print ( re.sub('(\n\n)\n*|\n', r'\1', s) )

Output:

hello

 My name is jeff

New to Python
  • Related