tags:

views:

66

answers:

3

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

A: 

Does it need to be a regex?

I'd just use

new_string = " ".join(re.split(s'\s+', old_string.strip()))
Ian Clelland
You still use a regular expression ;)
Felix Kling
I thought that as I was writing it out, actually :)
Ian Clelland
A: 

sed

 sed 's/[  ]\{2,\}/ /g'
jim mcnamara
+3  A: 
import re    
re.sub("\s+"," ",string)
Stedy