我有一個正則表達式來檢測任何電子郵件地址 - 我正在嘗試創建一個正則表達式,專門用於計算電子郵件地址並忽略來自特定域(abc.com)的電子郵件地址的電子郵件的標題。正則表達式來檢測電子郵件標題中的電子郵件地址數量?
例如,[email protected]有10個電子郵件地址,忽略來自[email protected]的第11個地址。
當前正則表達式:
^[A-Z0-9 ._%+ - ] + @ [A-Z0-9 .-] + [AZ] {2,4} $
我有一個正則表達式來檢測任何電子郵件地址 - 我正在嘗試創建一個正則表達式,專門用於計算電子郵件地址並忽略來自特定域(abc.com)的電子郵件地址的電子郵件的標題。正則表達式來檢測電子郵件標題中的電子郵件地址數量?
例如,[email protected]有10個電子郵件地址,忽略來自[email protected]的第11個地址。
當前正則表達式:
^[A-Z0-9 ._%+ - ] + @ [A-Z0-9 .-] + [AZ] {2,4} $
考慮下面的通用正則表達式的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-]+)*)
繼續查找字符塊後面跟着一個點我想嘗試在簡單的單個.string.find行中執行此操作....如果我可以將電子郵件地址的標題隔離起來,可以提供幫助 – user1181862 2013-05-03 03:14:58
已更新答案以允許使用單行正則表達式並且不需要邏輯 – 2013-05-03 03:36:40
非常感謝...會考驗。 – user1181862 2013-05-03 04:23:10
您使用什麼語言? – 2013-05-02 20:14:33
您是否知道根據RFC創建覆蓋所有有效電子郵件地址的正則表達式是不可能的? – 2013-05-02 20:52:57
不要爲此使用正則表達式。使用全面的電子郵件頭解析器,然後過濾所需的域(如果需要,使用正則表達式),並計算結果。 – Bergi 2013-05-02 21:53:11