Skip to content Skip to sidebar Skip to footer

Python Regex Remove New Lines (that Shouldn't Be There)

I got some text extracted and wish to clean it up by RegEx. I have learned basic RegEx but not sure how to build this one: str = ''' this is a line that has been cut. This is a li

Solution 1:

You can use this lookbehind regex for re.sub:

>>>str = '''...this is...a line that has been cut....This is a line that should start on a new line...'''>>>print re.sub(r'(?<!\.)\n', '', str)
this is a line that has been cut.
This is a line that should start on a new line
>>>

RegEx Demo

(?<!\.)\n matches all line breaks that are not preceded by a dot.

If you don't want a match based on presence of dot then use:

re.sub(r'(?<=\w\s)\n', '', str)

RegEx Demo 2

Post a Comment for "Python Regex Remove New Lines (that Shouldn't Be There)"