'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
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'
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anubhava |
