I am new to python. I am trying to clean texts Looking for a function that will do
regex_replace('\n{2,}', '\n\n', text);
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'
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
.
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