Matching horizontal white space

Magnus.Moraberg at gmail.com Magnus.Moraberg at gmail.com
Fri Sep 12 07:26:16 EDT 2008


multipleSpaces = re.compile(u'\\h+')

importantTextString = '\n  \n  \n \t\t  '
importantTextString = multipleSpaces.sub("M", importantTextString)

I would have expected consecutive spaces and tabs to be replaced by M
but nothing is being replaced. If I try the following, then I'm left
only with M, as expected -

multipleSpaces = re.compile(u'\\s+') # both vertical and horizontal

importantTextString = '\n  \n  \n \t\t  '
importantTextString = multipleSpaces.sub("M", importantTextString)


What I eventually wish to do is have only single spaces in my text and
to only have single carriage returns -

"    one   two     three          four

five


    six

"

becoming -

"one two three four
five
six
"

Thanks,

Barry



More information about the Python-list mailing list