String Split <img src> RRS feed

  • Question

  • User944339287 posted

    Hi guys.. below is my string. i wanna get all the <img src

    Dim collection as string = "Happy <img src=/upload/01.png /> Birthday <img src=/upload/02.jpg /> to my self"

    The value i wanna received. 
    1) <img src=/upload/01.png />
    2) <img src=/upload/02.jpg />

    Friday, April 19, 2019 5:20 AM

All replies

  • User288213138 posted

    Hi  kengkit,

        Based on your description, I tried to use regular expressions to split strings.

    The code:

    Private Sub SurroundingSub()
        Dim str As String = "Happy <img src=/upload/01.png /> Birthday <img src=/upload/02.jpg /> to my self"
        Dim r As Regex = New Regex("<img[\s\S]*?>", RegexOptions.IgnoreCase)
        Dim txt As String = String.Empty
        Dim col As MatchCollection = r.Matches(str)
        For Each item As Match In col
    End Sub

    The Result:

    Best Regards,


    Monday, April 22, 2019 2:41 AM
  • User3690988 posted

    If the origin of the list of images is a web page you could use the HtmlAgilityPack to find and parse for images: 

    Imports HtmlAgilityPack
    Public Class _default
      Inherits System.Web.UI.Page
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim web = New HtmlWeb
        Dim pageURL As String = "https://forums.asp.net/t/2154892.aspx?String+Split+img+src+"
        Dim document = web.Load(pageURL)
        Dim imagesOnPage = From imgs In document.DocumentNode.Descendants("img")
        Dim imglink As String = String.Empty
        Dim imgSRC As String = String.Empty
        For I As Int16 = 0 To imagesOnPage.Count - 1
          imglink += "<br />" & Server.HtmlEncode(imagesOnPage(I).OuterHtml.ToString)
          imgSRC += "<br/>" & Server.HtmlEncode(imagesOnPage(I).GetAttributeValue("src", ""))
        lblOut.Text = String.Format("Images found: {0}", imagesOnPage.Count() & imglink) & "<p>src:" & imgSRC & "</p>"
      End Sub
    End Class

    This will find the image links and also the src of the image:

    Images found: 4
    <img src='https://ads-asp.azureedge.net/production/images/azure-get-started.png' style='margin:0 auto; display:block; max-width:100%;'>
    <img class="post-thumb" src="/avatar/kengkit.jpg?dt=636915076200000000" alt="kengkit" title="kengkit">
    <img class="post-thumb" src="/avatar/samwu.jpg?dt=636915076200000000" alt="samwu" title="samwu">
    <img src="https://i.stack.imgur.com/Vq5Sl.png">

    Monday, April 22, 2019 12:42 PM