我在Web应用程序上的表单上有一个Webhook帖子,我需要解析电子邮件标题地址。

这是源文本:

Thread-Topic: test subject
Thread-Index: AcwE4mK6Jj19Hgi0SV6yYKvj2/HJbw==
From: "Lastname, Firstname" <firstname_lastname@domain.com>
To: <testto@domain.com>, testto1@domain.com, testto2@domain.com
Cc: <testcc@domain.com>, test3@domain.com
X-OriginalArrivalTime: 27 Apr 2011 13:52:46.0235 (UTC) FILETIME=[635226B0:01CC04E2]

我想撤出以下内容:

<testto@domain.com>, testto1@domain.com, testto2@domain.com

我整天都在努力,没有任何运气。

有帮助吗?

解决方案

与我必须同意mmutz的一些帖子相反,您不能用正则票据分析电子邮件...请参阅本文:

http://tools.ietf.org/html/rfc2822#section-3.4.1

3.4.1。 ADDR规格规范

ADDR-SPEC是一个特定的Internet标识符,包含本地解释的字符串,其次是At-Sign字符(“@”,ASCII值64),然后是Internet域。

“本地解释”的想法意味着只有接收服务器才能解析。

如果我要尝试解决这个问题,我会找到“到”的内容,将其分开,并尝试用system.net.mail.mailaddress分析每个段。

    static void Main()
    {
        string input = @"Thread-Topic: test subject
Thread-Index: AcwE4mK6Jj19Hgi0SV6yYKvj2/HJbw==
From: ""Lastname, Firstname"" <firstname_lastname@domain.com>
To: <testto@domain.com>, ""Yes, this is valid""@[emails are hard to parse!], testto1@domain.com, testto2@domain.com
Cc: <testcc@domain.com>, test3@domain.com
X-OriginalArrivalTime: 27 Apr 2011 13:52:46.0235 (UTC) FILETIME=[635226B0:01CC04E2]";

        Regex toline = new Regex(@"(?im-:^To\s*:\s*(?<to>.*)$)");
        string to = toline.Match(input).Groups["to"].Value;

        int from = 0;
        int pos = 0;
        int found;
        string test;

        while(from < to.Length)
        {
            found = (found = to.IndexOf(',', from)) > 0 ? found : to.Length;
            from = found + 1;
            test = to.Substring(pos, found - pos);

            try
            {
                System.Net.Mail.MailAddress addy = new System.Net.Mail.MailAddress(test.Trim());
                Console.WriteLine(addy.Address);
                pos = found + 1;
            }
            catch (FormatException)
            {
            }
        }
    }

从上述程序输出:

testto@domain.com
"Yes, this is valid"@[emails are hard to parse!]
testto1@domain.com
testto2@domain.com

其他提示

RFC 2822符合的电子邮件正则是:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

只需在文本上运行它,您就会获得电子邮件地址。

当然,总有一个选择不使用正则言论,而Regex不是最好的选择。但是要你!

您不能使用正则表达式来解析RFC2822邮件,因为它们的语法包含递归制作(在我的头顶上,这是为了评论 (a (nested) comment))使语法不规则。正则表达式(如名称所建议)只能解析 常规的 语法。

也可以看看 REGEX匹配打开标签除XHTML自包式标签外 了解更多信息。

正如盲人所暗示的那样,有时您可以以老式的方式解析它。

如果您愿意这样做,以下是一种快速方法,假设电子邮件标题文本称为“标题”:

int start = header.IndexOf("To: ");
int end = header.IndexOf("Cc: ");
string x = header.Substring(start, end-start);

我可能会在减法上的字节上失去了一个字节,但是您可以很容易地测试和修改。当然,您还必须确定您始终会在标题中有一个CC:行,否则这将无法使用。

有验证电子邮件与正则验证的细分 这里, ,其中引用了RFC 2822的更实际实施:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

看来您只希望“到”字段中的电子邮件地址,而且您也需要担心,因此以下类似以下内容可能会有效:

^To: ((?:\<?[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\>?,?(?:\s*))*)

再次,正如其他人提到的那样,您可能不想这样做。但是,如果您想要将该输入转化为 <testto@domain.com>, testto1@domain.com, testto2@domain.com, ,那会做到的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top