Regex是否检测电子邮件头中的电子邮件地址计数?
我有一个正则表达式来检测任何电子邮件地址-我正在尝试创建一个正则表达式,该正则表达式专门在电子邮件消息的标题中查找,统计电子邮件地址并忽略特定域(abc.com)中的电子邮件地址 例如,有十个来自的电子邮件地址1@test.com忽略来自的第11个地址2@abc.com. 当前正则表达式:Regex是否检测电子邮件头中的电子邮件地址计数?,regex,Regex,我有一个正则表达式来检测任何电子邮件地址-我正在尝试创建一个正则表达式,该正则表达式专门在电子邮件消息的标题中查找,统计电子邮件地址并忽略特定域(abc.com)中的电子邮件地址 例如,有十个来自的电子邮件地址1@test.com忽略来自的第11个地址2@abc.com. 当前正则表达式: ^[A-Z0-9.Z0%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$考虑以下通用正则表达式的powershell示例 要查找所有电子邮件地址,请执行以下操作: 如果您的服务器用括号括住电子邮件地
^[A-Z0-9.Z0%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$考虑以下通用正则表达式的powershell示例 要查找所有电子邮件地址,请执行以下操作:
- 如果您的服务器用括号括住电子邮件地址,
非常方便 (?如果标题中的所有电子邮件地址周围没有括号。请注意,此特定正则表达式是从上的社区wiki答案复制而来,并在此处进行了修改,以防止
。此正则表达式可能不适用于某些边缘情况。因此,在同一页面上,有一个非常复杂的正则表达式,看起来会匹配每个电子邮件地址。我没有时间修改这个地址,跳过@abc.com
@abc.com
(?防止
出现在电子邮件地址之前的10000000个字符内。这具有防止邮件正文中的电子邮件地址匹配的效果。因为请求者正在使用Java,Java不支持在我正在使用的内容类型
(另请参阅)。请注意,这可能会引入一些可能无法按预期捕获的边缘情况lookback中使用
{010000000}*
开始返回(
匹配1个或多个允许的字符。双单引号用于转义powershell的单引号字符。双倒勾用于转义stackoverflow的倒勾[a-zA-Z0-9.!\$%&'*+-/=?\^ `{124}-]+
包括第一个at符号@
如果查找包含(?!abc.com)
abc.com
继续查找字符串第一个点或末尾的所有剩余非贪婪字符[a-zA-Z0-9-]+
继续查找后跟点的字符块(?:\.[a-zA-Z0-9-]+)*)
到: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