Matching horizontal white space

Discussion in 'Python' started by Magnus.Moraberg@gmail.com, Sep 12, 2008.

  1. Guest

    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
     
    , Sep 12, 2008
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Sep 12, 2008
    #2
    1. Advertising

  3. John Machin Guest

    On Sep 13, 12:52 am, Fredrik Lundh <> wrote:
    > 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 :)
     
    John Machin, Sep 12, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,936
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,716
    Stephen Horne
    Oct 27, 2003
  3. Ben C
    Replies:
    6
    Views:
    2,185
    Leif K-Brooks
    Jan 28, 2007
  4. Robin

    matching only white space

    Robin, Jan 14, 2004, in forum: Perl Misc
    Replies:
    30
    Views:
    306
    Brian Helterline
    Jan 15, 2004
  5. erik
    Replies:
    10
    Views:
    182
    Anno Siegel
    Nov 11, 2005
Loading...

Share This Page