Matching horizontal white space

M

Magnus.Moraberg

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
 
F

Fredrik Lundh

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>
 
J

John Machin

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

Match *h*orizontal whitespace, I guess ... looks like the maintainer
of the re equivalent in some other language has far too much spare
time :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top