C# 检测http头中的电子邮件

C# 检测http头中的电子邮件,c#,regex,C#,Regex,我正在csharp中建立一个代理,我的任务之一是查找其中的电子邮件 一个http头,问题是在我得到的数据中我收到了%40而不是 @,有人能告诉我,当邮件地址中的@被替换为%40时,如何检测电子邮件吗? 这是我在给定字符串中获取电子邮件地址的代码(改为@而不是%40) 代码: string regexPattern = @"[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"; Regex regex = new Regex(regexPatter

我正在csharp中建立一个代理,我的任务之一是查找其中的电子邮件 一个http头,问题是在我得到的数据中我收到了%40而不是 @,有人能告诉我,当邮件地址中的@被替换为%40时,如何检测电子邮件吗? 这是我在给定字符串中获取电子邮件地址的代码(改为@而不是%40) 代码

  string regexPattern = @"[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
  Regex regex = new Regex(regexPattern);
  MatchCollection matches = regex.Matches(this._context.Request.Headers[i]);
  foreach (Match match in regex.Matches(this._context.Request.Headers[i]))
  {// any email address should be printed 
         Console.WriteLine(match.Value);
  }

不知道我是否理解你的问题。为什么不在您提供的regex中用
%40
替换
@
? 因此:

使用或(|)允许@或“%40”

[A-Za-z0-9.%-]+(@|%40)[A-Za-z0-9.-]+.[A-Za-z]{2,4}

在通过正则表达式运行数据之前:

regex.Matches(this._context.Server.UrlDecode(this._context.Request.Headers[i]))
或:

regex.Matches(this._context.Server.UrlDecode(this._context.Request.Headers[i]))
regex.Matches(HttpUtility.UrlDecode(this._context.Request.Headers[i]))