Matching horizontal white space

Fredrik Lundh fredrik at
Fri Sep 12 16:52:11 CEST 2008

Magnus.Moraberg at wrote:

> multipleSpaces = re.compile(u'\\h+')
> importantTextString = '\n  \n  \n \t\t  '
> importantTextString = multipleSpaces.sub("M", importantTextString)

what's "\\h" supposed to mean?

> I would have expected consecutive spaces and tabs to be replaced by M
> but nothing is being replaced.

if you know what you want to replace, be explicit:

 >>> importantTextString = '\n  \n  \n \t\t  '
 >>> re.compile("[\t ]+").sub("M", importantTextString)


More information about the Python-list mailing list