2010-06-11 6 views
3

J'ai stocké le contenu du courrier (corps du message) dans la base de données.
Je voudrais extraire la valeur de l'attribut "src" de l'ensemble tag d'image() de ces contenus de courrier.
Une ou plusieurs images peuvent être incluses dans le corps du message.Comment extraire la balise img dans Mail Body dans VB.NET

S'il vous plaît laissez-moi savoir comment je dois y parvenir dans VB.NET?
Merci.

Répondre

6

Vous pouvez utiliser une expression régulière .

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 

Voilà comment cela fonctionne:

<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 ""'" «["']» 
Questions connexes