Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Matching horizontal white space

Reply
Thread Tools

Matching horizontal white space

 
 
Magnus.Moraberg@gmail.com
Guest
Posts: n/a
 
      09-12-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
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-12-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      09-12-2008
On Sep 13, 12:52*am, Fredrik Lundh <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > multipleSpaces = re.compile(u'\\h+')

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

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any programs to trim white space/ remove all white space in HTML file? Ben C HTML 6 01-28-2007 11:41 PM
matching a pattern with a space or no space?? erik Perl Misc 10 11-11-2005 12:43 PM
matching only white space Robin Perl Misc 30 01-15-2004 09:54 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments