John John - 3 months ago 8
Vb.net 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
Text
which is inside
<data></data>
( .Replace won't help).

thanks

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
Else
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 (
ex:<data><str><text>...
)

Answer

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
Comments