2012-08-16 243 views
0

試圖歸檔正則表達式以篩選反彈的電子郵件,將它們與垃圾郵件或暫時無法投遞的郵件區分開來。正則表達式如果包含XXX但不包含XXX

我們的想法是抓住表達式可能包含的某些單詞(代碼+單詞),但如果它包含其他人(例如(SPAM |臨時無法傳送|磁盤配額超出)等等,則忽略整行),因爲這不會被視爲永久性反彈。我們已經管理了第一部分,並在這裏找到了一些有關負面正則表達式的答案(http://stackoverflow.com/questions/1153856/string-negation-using-regular-expressions),但是在一個組中混合完全不成功目前爲止的判決。

喜歡的東西:

.*(5.3.0|5.1.0).*(User unknown|invalid|Unknown address|doesn't have a) 

但不匹配其他任何地方,如果在同一行中包含XXX的話。喜歡的東西:

^(?!(SPAM|temporarily undeliverable|disk quota exceeded)).*$ 

因此,下面第一行會匹配,但第二個不應該

Diagnostic-Code: smtp; 5.3.0 - Other mail system problem 554-"delivery error: dd This user doesn't have a btinternet.com account ([email protected]) [0] - mta1000.bt.mail.ird.yahoo.com" (delivery attempts: 0)

Diagnostic-Code: smtp; 5.1.0 - Unknown address error 550-'RCPT TO: Mailbox disk quota exceeded' (delivery attempts: 0)

回答

2

你只在字符串爲您否定的開始搜索。你只需要添加一個.*

嘗試

^(?!.*(SPAM|temporarily undeliverable|disk quota exceeded)).*(5.3.0|5.1.0).*(User unknown|invalid|Unknown address|doesn't have a) 

看到它here on Regexr

+0

簡單;)謝謝。爲測試做了一些額外的改進和實際工作,似乎很好:http://regexr.com?31rp2 – luison 2012-08-16 11:37:44