John John - 1 year ago 72 Question

vb2008 match text between html tags

hello i'm using

Visual Basic 2008 Express Edition

how is it possible to match text between tags?

for example i have a string :
<data>Text</data>more text...
, how i can get the
which is inside
( .Replace won't help).


My solution :

Public Function parseText(ByVal str As String, ByVal tag As String) As String
Dim match As Match = Regex.Match(str, "<" & tag & "\b[^>]*>(.*?)</" & tag & ">")
If match.Groups.Count = 2 Then
Return match.Groups(1).Value
Return "0"
End If
End Function

I use this because in my case the tags will be always without
id, class, width, href, src, style ....
just tag name (

Answer Source

You can use RegularExpressions.

    Dim s As String = "<data>Hello world</data>"
    Dim match As Match = Regex.Match(s, "<data\b[^>]*>(.*?)</data>")

    Dim text As String

    If match.Groups.Count = 2 Then
        text = match.Groups(1).Value
    End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download