Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > regex search with a space as the fist character

Reply
Thread Tools

regex search with a space as the fist character

 
 
Daniel Santos
Guest
Posts: n/a
 
      09-17-2009
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 ?
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      09-17-2009
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
 
Reply With Quote
 
 
 
 
Jack Norton
Guest
Posts: n/a
 
      09-17-2009
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

 
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
Internet browsing Faild Sorry for the fist Qustion try this one =?Utf-8?B?TW9oYW1lZCBUYWhlcg==?= MCSE 1 01-08-2005 04:24 AM
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
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments