Skip to content
Advertisement

replace multiple whitespace with single space but keep new lines in regex match (python)

How can I clean up the following text with regex in python? I’m trying to replace multiple whitespaces and tabs with single spaces, but preserve new lines.

test = "This is a test. n Blah blah."

In my new string, I want it to be:

"This is a test. n Blah blah."

I’ve tried the following expression in an attempt to ignore new line characters, but it doesn’t seem to work.

result = re.sub('[^n]ss+', ' ', test)

Thanks!

Advertisement

Answer

You can use code below:

import re
test = "This      is a    test. n  Blah     blah."
print(re.sub(r"(?:(?!n)s)+", " ",test))

Output

This is a test. 
 Blah blah.
Advertisement