Parsing “From” addresses from email text

前端 未结 8 2096
春和景丽
春和景丽 2021-02-19 03:56

I\'m trying to extract email addresses from plain text transcripts of emails. I\'ve cobbled together a bit of code to find the addresses themselves, but I don\'t know how to mak

8条回答
  •  面向向阳花
    2021-02-19 04:52

    mailsrch = re.compile(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}')
    

    Expression breakdown:

    [\w-]: any word character (alphanumeric, plus underscore) or a dash

    [\w-.]+: any word character, a dash, or a period/dot, one or more times

    @: literal @ symbol

    [\w-][\w-.]+: any word char or dash, followed by any word char, dash, or period one or more times.

    [a-zA-Z]{1,4}: any alphabetic character 1-4 times.

    To make this match only lines starting with From:, and wrapped in < and > symbols:

    import re
    
    foundemail = []
    mailsrch = re.compile(r'^From:\s+.*<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>', re.I | re.M)
    foundemail.extend(mailsrch.findall(open('text.txt').read()))
    
    print foundemail
    

提交回复
热议问题