Best way to parse string of email addresses

后端 未结 13 2620
悲哀的现实
悲哀的现实 2021-02-14 04:10

So i am working with some email header data, and for the to:, from:, cc:, and bcc: fields the email address(es) can be expressed in a number of different ways:

F         


        
13条回答
  •  既然无缘
    2021-02-14 04:58

    You could use regular expressions to try to separate this out, try this guy:

    ^(?[a-zA-Z0-9]+?),? (?[a-zA-Z0-9]+?),? (?[a-zA-Z0-9.-_<>]+?)$
    

    will match: Last, First test@test.com; Last, First ; First last test@test.com; First Last . You can add another optional match in the regex at the end to pick up the last segment of First, Last , name@domain.com after the email address enclosed in angled braces.

    Hope this helps somewhat!

    EDIT:

    and of course you can add more characters to each of the sections to accept quotations etc for whatever format is being read in. As sjbotha mentioned, this could be difficult as the string that is submitted is not necessarily in a set format.

    This link can give you more information about matching AND validating email addresses using regular expressions.

提交回复
热议问题