Matching horizontal white space

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


Magnus.Moraberg at gmail.com 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)
'\nM\nM\nM'

</F>




More information about the Python-list mailing list