正则表达式检测电子邮件标题中的电子邮件地址数?

时间:2013-05-02 20:13:01

标签: regex

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

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

当前正则表达式:

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

1 个答案:

答案 0 :(得分:1)

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

查找所有电子邮件地址:

    如果您的服务器用括号包围电子邮件地址,则
  • <(.*?)>很方便
  • (?<!Content-Type(.|\n){0,10000000})([a-zA-Z0-9.!#$%&''*+-/=?\^_``{|}~-]+@(?!abc.com)[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)如果您的标题中的所有电子邮件地址都没有括号。请注意,此特定正则表达式是从stackoverflow 201323上的社区Wiki答案中复制而来,并在此处进行了修改,以防止@abc.com。可能有一些边缘情况,这个正则表达式不适用。所以在同一页面上有一个非常复杂的正则表达式,看起来它会匹配每个电子邮件地址。我没有时间修改那个跳过@abc.com

实施例

    $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

摘要

  • (?<!Content-Type(.|\n){0,10000000})阻止Content-Type出现在电子邮件地址之前的10,000,000个字符中。这具有防止消息正文中的电子邮件地址匹配的效果。因为请求者正在使用Java而Java不支持使用*内部的后卫,而是使用{0,10000000}。 (另见Regex look behind without obvious maximum length in Java)。请注意,这可能会引入一些可能无法按预期捕获的边缘情况。
  • <(.*?@(?!abc.com).*?)>
    • (开始返回
    • [a-zA-Z0-9.!#$%&''*+-/=?\^_``{|}~-]+匹配1个或多个允许的字符。双单引号是为了逃避powershell的单引号字符。并且双后退勾选了stackoverflow的反引号。
    • @包含第一个签名
    • 如果
    • (?!abc.com)包含abc.com
    • ,则拒绝该搜索
    • [a-zA-Z0-9-]+继续寻找非贪婪的所有剩余字符,直到字符串的第一个点或结尾。
    • (?:\.[a-zA-Z0-9-]+)*)继续寻找字符块后跟一个点