User19
User19

Reputation: 121

How to replace repeated new lines with one removing single new lines?

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

Upvotes: 1

Views: 447

Answers (1)

anubhava
anubhava

Reputation: 784958

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

Upvotes: 2

Related Questions