Regexps problem

Bengt Richter bokr at oz.net
Thu Dec 12 15:26:00 EST 2002


On Thu, 12 Dec 2002 15:46:51 +0100, Duff <bruno.dusausoy at skynet.be> wrote:

>On Thu, 12 Dec 2002 15:38:40 +0100, Duff wrote:
>
>I've forgot to include an example ...
>Here it is:
Bravo ;-)
>
>
>>>>server.select()
>>>>('OK', ['(\\HasNoChildren) "." "INBOX.Test.Sous Test"',
>'(\\HasChildren) "." "INBOX.Test"', '(\\HasNoChildren) "."
>"INBOX.Trash"', '(\\Unmarked \\HasChildren) "." "INBOX"'])
>>>>

Pretend you had written
     data = server.select()
so this should be equivalent to your example

 >>> data = ('OK',[
 ... '(\\HasNoChildren) "." "INBOX.Test.Sous Test"',
 ... '(\\HasChildren) "." "INBOX.Test"',
 ... '(\\HasNoChildren) "." "INBOX.Trash"',
 ... '(\\Unmarked \\HasChildren) "." "INBOX"'
 ... ])

The second tuple element is apparently a list of strings ending in the quoted string you
are interested in. IF you can assume that it's quoted with double quote characters and has
no escaped double quote embedded, it looks like you could do this without regexp's:

Just for reference, see what we get splitting the strings with '"':
 >>> [s.split('"') for s in data[1]]
 [['(\\HasNoChildren) ', '.', ' ', 'INBOX.Test.Sous Test', ''], ['(\\HasChildren) ', '.', ' ', 'I
 NBOX.Test', ''], ['(\\HasNoChildren) ', '.', ' ', 'INBOX.Trash', ''], ['(\\Unmarked \\HasChildre
 n) ', '.', ' ', 'INBOX', '']]

Then pick the part you want:

 >>> [s.split('"')[-2] for s in data[1]]
 ['INBOX.Test.Sous Test', 'INBOX.Test', 'INBOX.Trash', 'INBOX']

Or print it as you specify:

 >>> for x in [s.split('"')[-2] for s in data[1]]: print x
 ...
 INBOX.Test.Sous Test
 INBOX.Test
 INBOX.Trash
 INBOX

>
>I don't care about the first tuple element ('OK')
>I want to have : 
>INBOX.Test.Sous Test
>INBOX.Test
>INBOX.Trash
>INBOX
>
>> Hi,
>> 
>> I use the imaplib module to communicate with my IMAP4 server.
>> 
>> Whe i ask the server which mailboxes are there, it returns a tuple
>> containing a string and a list of mailboxes (with their attributes).
>> What interest me is this list. Each list item is a string It contains 3
>> "parts" :
>> - mailbox attributes consisting of one or more words (separated by a
>> whitespace )preceded by '\' and encapsulated by two parenthesis '(' and
>> ')' Ex : (\Haschildren \Unmarked) or (\Hasnochildren) - hierarchy
>> delimiter consisting in one character (it could be any character)
>> encapsulated by two "  (Example : "." or "/") - mailbox name :
>> consisting in a string encapsulated by two " (Example : "Test" or
>> "Test.SubTest")
>>
The example made it a lot easier ;-)
 
>> What i'd like to do is isolate the mailboxes names. How can I do that
>> with regexps ?
Probably don't need them unless you have escaped quotes inside the quoted values
or have possibility that it's quoted with ['] instead of ["].

Regards,
Bengt Richter



More information about the Python-list mailing list