Search code examples
htmlvb.nethtml-agility-packinnertext

Get Inner text between two tags and get the output into two labels - VB.NET - HtmlAgilityPack


I tried to search for examples and much but nothing seems to work. so i'm using HtmlAgilityPack and I want to get the inner text between two specific tags.

Example:

<br>Terms of Service<br></br>Developers<br>

I want to get the innertext where first <br> and <br> into label1 and the second </br> and <br> into label2

which will be like

Label1.text = "Terms of Service"
Label2.text = "Developers"

How do i achieve/do/get this? P.s; I'm not so familiar with HtmlAgilityPack, a code showing how to do this will do better. :-)

Thanks


Solution

  • this is a bit dirty, but should work.

    Imports System.Text.RegularExpressions
    
      Dim mystring As String = "<br>Terms of Service<br></br>Developers<br>"
    
        Dim pattern1 As String = "(?<=<br>)(.*?)(?=<br>)"
        Dim pattern2 As String = "(?<=</br>)(.*)(?=<br>)"
    
        Dim m1 As MatchCollection = Regex.Matches(mystring, pattern1)
        Dim m2 As MatchCollection = Regex.Matches(mystring, pattern2)
        MsgBox(m1(0).ToString)
        MsgBox(m2(0).ToString)