2010-06-11 29 views
3

我將郵件內容(郵件正文)存儲在數據庫中。
我想從這些郵件內容中提取所有圖像標記()的「src」屬性的值。
郵件正文可能包含一個或多個圖片。如何在VB.NET中提取郵件正文中的img標記

請讓我知道如何在VB.NET中實現這個功能?
謝謝。

回答

6

您可以使用正則表達式

Try 
    Dim RegexObj As New Regex("<img[^>]+src=[""']([^""']+)[""']", RegexOptions.Singleline Or RegexOptions.IgnoreCase) 
    Dim MatchResults As Match = RegexObj.Match(SubjectString) 
    While MatchResults.Success 
     ' SRC attribute is in MatchResults.Groups(1).Value 
     MatchResults = MatchResults.NextMatch() 
    End While 
Catch ex As ArgumentException 
    'Syntax error in the regular expression (which there isn't) 
End Try 

下面是它如何工作的:

<img[^>]+src=["']([^"']+)["'] 

Match the characters "<img" literally «<img» 
Match any character that is not a ">" «[^>]+» 
    Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
Match the characters "src=" literally «src=» 
Match a single character present in the list ""'" «["']» 
Match the regular expression below and capture its match into backreference number 1 «([^"']+)» 
    Match a single character NOT present in the list ""'" «[^"']+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
Match a single character present in the list ""'" «["']» 
相關問題