Answer the question
In order to leave comments, you need to log in
How to copy words between certain characters?
I need to run a command on every mail server folder. In issue received the list of folders. As far as I figured out, the folder name is between the symbols "/" " and "'
But somehow I can’t seem to get these names into a list :(
I tried to shaman with the re module, something doesn’t work :(
import re
string = """('OK', [b'(\\Inbox) "/" "INBOX"', b'() "/" "&BCIENQRBBEIEPgQyBEsENQ- &BEIENQQ7BDUEMwRABDAEPAQ8BEs-"', b'(\\Spam) "/" "&BCEEPwQwBDw-"', b'(\\Sent) "/" "&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-"', b'(\\Drafts) "/" "&BCcENQRABD0EPgQyBDgEOgQ4-"', b'(\\Trash) "/" "&BBoEPgRABDcEOAQ9BDA-"', b'(\\NoInferiors) "/" "INBOX/Social"', b'(\\NoInferiors) "/" "INBOX/News Letters"'])"""
string = re.findall(r'"/" "+\D+"\'', string)
print(string)
Answer the question
In order to leave comments, you need to log in
Why re? It is not necessary to import a special module - I think it is possible to assemble such a list with standard splits or particip ...
With the help of partition, you can quickly do this:
string = """('OK', [b'(\\Inbox) "/" "INBOX"', b'() "/" "&BCIENQRBBEIEPgQyBEsENQ- &BEIENQQ7BDUEMwRABDAEPAQ8BEs-"', b'(\\Spam) "/" "&BCEEPwQwBDw-"', b'(\\Sent) "/" "&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-"', b'(\\Drafts) "/" "&BCcENQRABD0EPgQyBDgEOgQ4-"', b'(\\Trash) "/" "&BBoEPgRABDcEOAQ9BDA-"', b'(\\NoInferiors) "/" "INBOX/Social"', b'(\\NoInferiors) "/" "INBOX/News Letters"'])"""
res = []
st_rest = string.partition('''/" "''')[2]
while st_rest != '':
part = st_rest.partition(""""'""")
res.append(part[0])
st_rest = part[2].partition('''/" "''')[2]
for r in res: print(r)
# INBOX
# &BCIENQRBBEIEPgQyBEsENQ- &BEIENQQ7BDUEMwRABDAEPAQ8BEs-
# &BCEEPwQwBDw-
# &BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-
# &BCcENQRABD0EPgQyBDgEOgQ4-
# &BBoEPgRABDcEOAQ9BDA-
# INBOX/Social
# INBOX/News Letters
... you can probably write more sensibly, but, as for me, there is no problem at all. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question