2010-04-29 86 views
1

我需要突出顯示文本中的電子郵件地址,但如果包含在HTML標記,內容或屬性中,則不會突出顯示它們。匹配電子郵件地址不包含在HTML標記中

例如,串[email protected]必須不能被處理的字符串中<a href="mailto:[email protected]">[email protected]</a>轉換爲<a href="mailto:[email protected]">[email protected]</a>

但是電子郵件地址。

我已經試過這樣的正則表達式:

(?<![":])[a-zA-Z0-9._%-+][email protected][a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}(?!")

,但它不能正常工作。

+0

重複:http://stackoverflow.com/questions/401726/regex-that-only-matches-text-thats-not-part-of-html-markup-python – msw 2010-04-29 02:30:12

回答

1

我會猜測你的源文本是一個HTML文件,它只包含了一些包含的電子郵件地址的錨定標記。如果這是真的,那麼您將無法使用正則表達式來可靠匹配未標記的電子郵件地址。例如,給定輸入:

... 
<P>You'll find a lot more written by <A 
href="mailto:[email protected]" 
title="some text including [email protected]"> 

[email protected] 
</A>. 
</P> 
... 

就不可能詞彙的href與地址相關聯,也排除[email protected]。您需要使用HTML解析器; BeautifulSoup很受歡迎。

相關問題