Regex是否检测电子邮件头中的电子邮件地址计数?

Regex是否检测电子邮件头中的电子邮件地址计数?,regex,Regex,我有一个正则表达式来检测任何电子邮件地址-我正在尝试创建一个正则表达式,该正则表达式专门在电子邮件消息的标题中查找,统计电子邮件地址并忽略特定域(abc.com)中的电子邮件地址 例如,有十个来自的电子邮件地址1@test.com忽略来自的第11个地址2@abc.com. 当前正则表达式: ^[A-Z0-9.Z0%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$考虑以下通用正则表达式的powershell示例 要查找所有电子邮件地址,请执行以下操作: 如果您的服务器用括号括住电子邮件地

我有一个正则表达式来检测任何电子邮件地址-我正在尝试创建一个正则表达式,该正则表达式专门在电子邮件消息的标题中查找,统计电子邮件地址并忽略特定域(abc.com)中的电子邮件地址

例如,有十个来自的电子邮件地址1@test.com忽略来自的第11个地址2@abc.com.

当前正则表达式:


^[A-Z0-9.Z0%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$

考虑以下通用正则表达式的powershell示例

要查找所有电子邮件地址,请执行以下操作:

  • 如果您的服务器用括号括住电子邮件地址,
    非常方便
  • (?如果标题中的所有电子邮件地址周围没有括号。请注意,此特定正则表达式是从上的社区wiki答案复制而来,并在此处进行了修改,以防止
    @abc.com
    。此正则表达式可能不适用于某些边缘情况。因此,在同一页面上,有一个非常复杂的正则表达式,看起来会匹配每个电子邮件地址。我没有时间修改这个地址,跳过
    @abc.com
例子 总结
  • (?防止
    内容类型
    出现在电子邮件地址之前的10000000个字符内。这具有防止邮件正文中的电子邮件地址匹配的效果。因为请求者正在使用Java,Java不支持在我正在使用的
    lookback中使用
    *
    {010000000}
    (另请参阅)。请注意,这可能会引入一些可能无法按预期捕获的边缘情况
    • 开始返回
    • [a-zA-Z0-9.!\$%&'*+-/=?\^ `{124}-]+
      匹配1个或多个允许的字符。双单引号用于转义powershell的单引号字符。双倒勾用于转义stackoverflow的倒勾
    • @
      包括第一个at符号
    • (?!abc.com)
      如果查找包含
      abc.com
    • [a-zA-Z0-9-]+
      继续查找字符串第一个点或末尾的所有剩余非贪婪字符
    • (?:\.[a-zA-Z0-9-]+)*)
      继续查找后跟点的字符块

您使用的是什么语言?您是否意识到,不可能按照RFC创建覆盖所有有效电子邮件地址的正则表达式?不要使用正则表达式。请使用完整的电子邮件头解析器,然后筛选您想要的域(如果需要,请使用正则表达式),并计算结果。我正在使用Java正则表达式引擎此链接有一个非常好的正则表达式用于查找电子邮件地址,同时不使用任何Java中不稳定的lookbehind。我想尝试在一个简单的.string.find行中实现这一点……如果我可以隔离电子邮件地址的标题,这有助于更新answer允许单行正则表达式,无逻辑。多谢…将测试。地址不保证在楔形中;
到:billg@example.com
是完全有效的。测试后,邮件系统似乎没有将周围的电子邮件地址添加为“TO”和“CC”
    $Matches = @()
    $String = 'Return-Path: <example_from@abc123.com>
X-SpamCatcher-Score: 1 [X]
Received: from [136.167.40.119] (HELO abc.com)
    by fe3.abc.com (CommuniGate Pro SMTP 4.1.8)
    with ESMTP-TLS id 61258719 for example_to@mail.abc.com;
Message-ID: <4129F3CA.2020509@abc.com>
Date: Wed, 21 Jan 2009 12:52:00 -0500 (EST)
From: Taylor Evans <Remember@To.Vote>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.1)
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: Jon Smith <example_to@mail.abc.com>
Subject: Business Development Meeting
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Content-Type: multipart/alternative;
boundary="------------060102080402030702040100"
This is a multi-part message in MIME format.
--------------060102080402030702040100
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Hello,
this is an HTML mail, it has *bold*, /italic /and _underlined_ text.
And then we have a table here:
Cell(1,1)
Cell(2,1)
Cell(1,2) Cell(2,2)
And we put a picture here:
Image Alt Text
That''s it.
--------------060102080402030702040100
Content-Type: multipart/related;
boundary="------------030904080004010009060206"
--------------030904080004010009060206
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-15">
</head>
<body bgcolor="#ffffff" text="#000000">
Hello,<br>
<br>
this is an HTML mail, it has <b>bold</b>, <i>italic </i>and <u>underlined</u>
text.<br>
And then we have a table here:<br>
<table border="1" cellpadding="2" cellspacing="2" height="62"
width="401">
<tbody>
<tr>
<td valign="top">Cell(1,1)<br>
</td>
<td valign="top">Cell(2,1)</td>
</tr>
<tr>
<td valign="top">Cell(1,2)</td>
<td valign="top">Cell(2,2)</td>
</tr>
</tbody>
</table>
<br>
And we put a picture here:<br>
<br>
<img alt="Image Alt Text"
src="cid:part1.FFFFFFFF.5555555@example.com" height="79"
width="98"><br>
<br>
That''s it. email me at test@email.com<br>
Subject: <br>
</body>
</html>'

    # Write-Host start with 
# write-host $String
Write-Host
Write-Host found
[array]$Found = ([regex]'(?<!Content-Type(.|\n){0,10000000})([a-zA-Z0-9.!#$%&''*+-/=?\^_`{|}~-]+@(?!abc.com)[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)').matches($String) 

$Found | foreach {
    write-host "key at $($_.Groups[1].Index) = '$($_.Groups[1].Value)'"
    } # next match
Write-Host "found $($Found.count) matching addresses"
found
key at 14 = 'example_from@abc123.com'
key at 200 = 'example_to@mail.abc.com'
key at 331 = 'Remember@To.Vote'
key at 485 = 'example_to@mail.abc.com'
found 4 matching addresses