Home > Enterprise >  Splitting Account Name from email address
Splitting Account Name from email address

Time:07-12

I've search thru the net but I saw splitting email addresses which doesn't satisfy my needs.
Basically imaplib returns the sender's email as

Account Name <[email protected]>

I need to extract the email address only because that's what the only thing I'm going to use. Kinda new to regex, so insights will be awesome, Thanks in advance!

My code to extract sender details incase it's needed

for i in range (1, messages 1,  1):
    # fetch the email message by ID
    res, msg = mail.fetch(str(i), "(BODY.PEEK[HEADER.FIELDS (FROM SUBJECT)])")
    for response in msg:
        if isinstance(response, tuple):
            # parse a bytes email into a message object
            msg = email.message_from_bytes(response[1])
            # decode email sender
            From, encoding = decode_header(msg.get("From"))[0]
            if isinstance(From, bytes):
                From = From.decode(encoding)
            print("From:", From)

CodePudding user response:

Test this expression if it works:

(?!<)[\w_@.] (?=>)

Here is my test:

enter image description here

CodePudding user response:

There are several ways how to achieve this.

You can use the library Pyparsing. Once you have defined a grammar you can use the function scan_string to extract only the information you need. More information about Pyparsing you can finde here

Regular expressions can also be used. Regular expressions are often not the best approach because they are hard to read. Many good developers in Python don't use Regular expressions a lot. But in your example it should be relatively simple. A good source of information for regular expressions is here.

You can also use hand written code. In your case the code shouldn't be too complicated as you only have to identify the < and > characters. A few words about a similar problem you can find here

  • Related