Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the img tag in Mail Body in VB.NET

Tags:

vb.net

I stored the mail contents(mail body) in database.
I would like to extract the value of "src" attribute of the all image tag() from those mail contents.
One or more image may be included in mail body.

Please let me know how I have to acheive this in VB.NET?
Thanks.

like image 612
Kai Avatar asked Dec 03 '25 19:12

Kai


1 Answers

You can use a regular expression.

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

Here's how it works:

<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 ""'" «["']»
like image 78
Ben H Avatar answered Dec 06 '25 14:12

Ben H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!