-1

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 <emailaddress@gmail.com>

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)
CSAPawn
  • 47
  • 6
  • So what is your actual question. What is the point where you run into issues? (Also, to test regular expressions, try [this site](https://regex101.com/).) – Felix Jul 12 '22 at 06:01
  • 1
    As the imalib return this format when extracting email details `Account Name ` I want to extract the email address only without the account name – CSAPawn Jul 12 '22 at 06:03
  • 1
    Also, the Question [How can I validate an email address using a regular expression?](https://stackoverflow.com/q/201323/15432738) thoroughly covers the topic of matching e-mail addresses using regular expressions. – Felix Jul 12 '22 at 06:29

2 Answers2

1

Test this expression if it works:

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

Here is my test:

enter image description here

I am using RegExr to test this regex: https://regexr.com/

1

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

habrewning
  • 735
  • 3
  • 12