[Tutor] uncomprehension on RE

cedric briner work at infomaniak.ch
Thu Sep 20 08:12:30 CEST 2007


Kent Johnson wrote:
> cedric briner wrote:
>> Hello,
>>
>> I do not understand the behaviour of this:
>>
>> import re
>> re.search('(a)*','aaa').groups()
>> ('a',)
>>
>> I was thinking that the ``*'' will operate on the group delimited by the 
>> parenthesis. And so, I was expecting this result:
>> ('a', 'a', 'a')
>>
>> Is there something I'am missing ?
> 
> It just doesn't work that way.
that's sad :(

> The returned group is the last string 
> matched by the group:
> In [17]: re.search('(a.)*','azabac').groups()
> Out[17]: ('ac',)
Somewhere I find re really too difficult. I was never able to write an 
re expression rightly from the first shoot ! Happily there is kodos and 
redet.

> Use re.findall() instead:
> In [18]: re.findall('a.', 'azabac')
> Out[18]: ['az', 'ab', 'ac']
that's what I found, that I've to do the job with many python steps than 
just one

> If you want overlapping matches you have to search in a loop and collect 
> them yourself, findall() gives only non-overlapping matches.


To let you know, I'm writing a script to generate bind9 configuration 
from a nis hosts table. So I was trying in a one re to catch from this:

<ip> <hostname> [ <cname> ...] [# comment]
e.g:
10.12.23.45 hostname1 alias1 alias2 alias3 # there is a nice comment
37.64.86.23 hostname2
35.25.89.34 hostname3 alias5
.. ..


so I was wishing to write an re expresion which will do in a one step 
all the job ! maybe am'I expecting too much from re :)


Ced.

-- 

Cedric BRINER
Geneva - Switzerland


More information about the Tutor mailing list