pythonstringlistpython-reimapi

Extracting string from ann email body


I'm using python to extract the information provided from the body of an email using imap.

Part of the email that interests to my code: "BOT ID: 4824CF8B-2986-11EC-80F0-84A93851B964"

I can extract the exact string from the email body with

    if content_type == "text/plain" and "attachment" not in content_disposition:
        import re
        ID_pattern = r"BOT ID: (\w+)-(\w+)-(\w+)-(\w+)-(\w+)"
        machine_id = re.findall(ID_pattern, body)
        print(machine_id)

But it returns: "[('4824CF8B', '2986', '11EC', '80F0', '84A93851B964')]"

How can I manipulate this turple/list to keep que original pattern: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx

Thank u all guys.

import re
ID_pattern = r"BOT ID: (\w+)-(\w+)-(\w+)-(\w+)-(\w+)"
machine_id = re.findall(ID_pattern, body)
print(machine_id)

result: [('4824CF8B', '2986', '11EC', '80F0', '84A93851B964')]

Expected: 4824CF8B-2986-11EC-80F0-84A93851B964


Solution

  • Use [\w\-] to denote word-constituting character or dash, that is

    import re
    text = "foo BOT ID: 4824CF8B-2986-11EC-80F0-84A93851B964 bar"
    for found in re.findall(r"BOT ID: ([\w\-]+)", text):
        print(found)
    

    gives output

    4824CF8B-2986-11EC-80F0-84A93851B964