2008年4月18日

HOW TO:辨識 Visual Basic HTML 字串中的文字

資料來源處:http://msdn2.microsoft.com/zh-tw/library/ms235379(VS.80).aspx
HOW TO:辨識 Visual Basic HTML 字串中的文字

這個範例會使用簡單的規則運算式 (Regular Expression) 移除 HTML 文件的標記。

範例
HTML 標記會與規則運算式 \<[^\>]+\> 相符,這表示:

字元 "<",之後接著 一或多個字元集合,不包括 ">" 字元,之後接著

字元 ">"。

這個範例會使用共用的 System.Text.RegularExpressions.Regex.Replace(System.String,System.String,System.String) 方法,以使用空字串取代標記規則運算式的所有符合項。

''' Removes the tags from an HTML document.
''' HTML text to parse.
''' The text of an HTML document without tags.
'''
Function GetTextFromHtml(ByVal htmlText As String) As String
Dim output As String = Regex.Replace(htmlText, "\<[^\>]+\>", "")
Return output
End Function

這個範例要求您必須使用 Imports 陳述式,匯入 System.Text.RegularExpressions 命名空間。如需詳細資訊,請參閱 Imports 陳述式