簡體   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