繁体   English   中英

获取HTML标记内的字符串-VB.Net

[英]Get string inside html tag - VB.Net

所以,我有这个HTML代码:

<div class="keyboard">
  <p>
    Hello world!
  </p>
</div>

我想输入文本“ Hello world!”。 我在下面尝试了我的正则表达式代码,但是没有用。

Dim findtext2 As String = "(?<=<div class=""keyboard"">)(.*?)(?=</div>)"
Dim myregex2 As String = TextBox1.Text 'HTML code above
Dim doregex2 As MatchCollection = Regex.Matches(myregex2, findtext2)
Dim matches2 As String = ""
For Each match2 As Match In doregex2
    matches2 = matches2 + match2.ToString + Environment.NewLine
Next
MsgBox(matches2)

正如评论中提到的,请勿使用ReGex解析html代码。
而是使用LINQ to XML

Dim html As XElement =
    <html>
        <body>
            <div class = "keyboard">
                <p>Hello word!</p>
            </div>
        </body>
    </html>

Dim values As String = 
    html.Descendants("div").
         Where(Function(div) div.Attribute("class").Value.Equals("keyboard")).
         Select(Function(div) div.Element("p").Value)

For Each value As String in values
    Console.WriteLine(value);
End For

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM