Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone know a good regex to remove extra whitespace? [duplicate]

Tags:

python

regex

Possible Duplicate:
Substitute multiple whitespace with single whitespace in Python

trying to figure out how to write a regex that given the string:

"hi     this       is a  test"

I can turn it into

"hi this is a test"

where the whitespace is normalized to just one space

any ideas? thanks so much

like image 748
James Avatar asked Dec 17 '25 18:12

James


1 Answers

import re    
re.sub("\s+"," ",string)
like image 113
Stedy Avatar answered Dec 20 '25 10:12

Stedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!