2013-05-02 72 views
1

我有一个正则表达式来检测任何电子邮件地址 - 我正在尝试创建一个正则表达式,专门用于计算电子邮件地址并忽略来自特定域(abc.com)的电子邮件地址的电子邮件的标题。正则表达式来检测电子邮件标题中的电子邮件地址数量?

例如,[email protected]有10个电子邮件地址,忽略来自[email protected]的第11个地址。

当前正则表达式:

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

+0

您使用什么语言? – 2013-05-02 20:14:33

+6

您是否知道根据RFC创建覆盖所有有效电子邮件地址的正则表达式是不可能的? – 2013-05-02 20:52:57

+3

不要为此使用正则表达式。使用全面的电子邮件头解析器,然后过滤所需的域(如果需要,使用正则表达式),并计算结果。 – Bergi 2013-05-02 21:53:11

回答

1

考虑下面的通用正则表达式的PowerShell示例。

要找到的所有电子邮件地址:

  • <(.*?)>如果你的服务器与周围的括号
  • (?<!Content-Type(.|\n){0,10000000})([a-zA-Z0-9.!#$%&''*+-/=?\^_``{|}~-][email protected](?!abc.com)[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)的电子邮件地址,如果你没有周围的所有电子邮件地址,括号中的标题更是得心应手。请注意,这个特定的正则表达式是从社区维基答案stackoverflow 201323复制而来,并在此修改以防止@abc.com。有可能是一些边界案件,这正则表达式不会工作。因此,在同一页面上有非常复杂的正则表达式,看起来它会匹配每个电子邮件地址。我没有时间去修改那个跳过@abc.com

$Matches = @() 
    $String = 'Return-Path: <[email protected]> 
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 [email protected]; 
Message-ID: <[email protected]> 
Date: Wed, 21 Jan 2009 12:52:00 -0500 (EST) 
From: Taylor Evans <[email protected]> 
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 <[email protected]> 
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:[email protected]" height="79" 
width="98"><br> 
<br> 
That''s it. email me at [email protected]<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.!#$%&''*+-/=?\^_`{|}~-][email protected](?!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 = '[email protected]' 
key at 200 = '[email protected]' 
key at 331 = '[email protected]' 
key at 485 = '[email protected]' 
found 4 matching addresses 

摘要
  • (?<!Content-Type(.|\n){0,10000000})防止Content-Type从电子邮件地址前的10,000,000个字符中出现。这具有防止邮件正文中的电子邮件地址匹配的效果。因为请求者正在使用Java,并且Java不支持在向后看中使用*,所以我使用的是{0,10000000}。 (另请参阅Regex look behind without obvious maximum length in Java)。请注意,这可能会引入一些可能无法按预期捕获的边缘案例。
  • <(.*[email protected](?!abc.com).*?)>
    • (开始回归
    • [a-zA-Z0-9.!#$%&''*+-/=?\^_``{|}~-]+匹配1个或多个不允许的字符。双单引号是为了逃避powershell的单引号字符。并且双后退勾号逃避了stackoverflow的倒退。
    • @包括第一at符号
    • (?!abc.com)拒绝找到,如果它包括abc.com
    • [a-zA-Z0-9-]+继续寻找其余所有人物非贪婪高达第一个点或字符串的结尾。
    • (?:\.[a-zA-Z0-9-]+)*)继续查找字符块后面跟着一个点
+0

我想尝试在简单的单个.string.find行中执行此操作....如果我可以将电子邮件地址的标题隔离起来,可以提供帮助 – user1181862 2013-05-03 03:14:58

+0

已更新答案以允许使用单行正则表达式并且不需要逻辑 – 2013-05-03 03:36:40

+0

非常感谢...会考验。 – user1181862 2013-05-03 04:23:10

相关问题