【发布时间】:2017-02-02 06:12:40
【问题描述】:
所以,我有这个 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)
【问题讨论】:
-
在我看来,您在第 2 行缺少结束引号。实际上不是 .NET 专家,所以我可能是错的:P
-
' 字符用于 cmets,不需要结尾。
-
嗯,这很令人困惑。在基本上所有其他语言中,它要么是语法错误,要么是字符串/字符文字。
-
忽略那条评论,它是给人们阅读代码的,它也不影响它。
-
用正则表达式提取 html 很麻烦,而且容易出错。也许试试the html agility pack。或者,如果它是 xhtml,则使用 xml api 之一