regex search with a space as the fist character

Discussion in 'Python' started by Daniel Santos, Sep 17, 2009.

  1. Hello,

    >>> print re.compile('u ').search(" u box2", 1)

    <_sre.SRE_Match object at 0x7ff1d918>
    >>> print re.compile(' u ').search(" u box2", 1)

    None


    Why ?
    Daniel Santos, Sep 17, 2009
    #1
    1. Advertising

  2. Daniel Santos schrieb:
    > Hello,
    >
    >>>> print re.compile('u ').search(" u box2", 1)

    > <_sre.SRE_Match object at 0x7ff1d918>
    >>>> print re.compile(' u ').search(" u box2", 1)

    > None
    >
    >
    > Why ?


    because you start searching at the offset 1, which means you try to find
    " u " in "u box2" - and that's not found.

    Diez
    Diez B. Roggisch, Sep 17, 2009
    #2
    1. Advertising

  3. Daniel Santos

    Jack Norton Guest

    Diez B. Roggisch wrote:
    > Daniel Santos schrieb:
    >> Hello,
    >>
    >>>>> print re.compile('u ').search(" u box2", 1)

    >> <_sre.SRE_Match object at 0x7ff1d918>
    >>>>> print re.compile(' u ').search(" u box2", 1)

    >> None
    >>
    >>
    >> Why ?

    >
    > because you start searching at the offset 1, which means you try to
    > find " u " in "u box2" - and that's not found.
    >
    > Diez

    Couldn't you also just do:
    re.compile('^\su')? That would match only if the first character is a
    space and the second a 'u'.
    I mean, I like to try and have the regexp do it all, instead of forcing
    an index, not to mention trying to find the most specific regexp that
    works(least generic -- I don't want any false positives). I also do
    _not_ like having spaces in the regexp itself. If I need more than one
    space I'll use '\s*' or '\s+'.
    Also, whats the point of compiling the regexp if you use it only once?

    -Jack
    Jack Norton, Sep 17, 2009
    #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. ad
    Replies:
    3
    Views:
    1,009
    Alex Smith
    Jun 8, 2005
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,890
    Bryan Bullard
    Jul 11, 2003
  3. Radu
    Replies:
    1
    Views:
    283
  4. sci.math
    Replies:
    0
    Views:
    197
    sci.math
    Jun 23, 2010
  5. Wen
    Replies:
    7
    Views:
    174
    Dave Anderson
    Nov 19, 2004
Loading...

Share This Page