标题:去除过滤html代码的正则表达式-excel vba中要用到的提取html中文字汉字

-------------------------------------------------------------------------------------------------------------------------------

时间:2011/12/24 0:45:18

-------------------------------------------------------------------------------------------------------------------------------

内容:

在抓取网页处理源代码要遇到的就是过滤html 提取html中的文字

先有2种方法 一个是使用正则表达式经过测试 用循环的是用正则效率的一万遍以上

Function RemoveHTML( strText )
Set regex = CreateObject("VBScript.RegExp")
RegEx.Pattern = "<[^>]*>"
RegEx.Global = True
RemoveHTML = RegEx.Replace(strText, "")
End Function

还有一种不用正则表达式的

 

Function RemoveHTML(strText)
    Dim nPos1
    Dim nPos2
   
    nPos1 = InStr(strText, "<")
    Do While nPos1 > 0
        nPos2 = InStr(nPos1 + 1, strText, ">")
        If nPos2 > 0 Then
            strText = Left(strText, nPos1 - 1) & Mid(strText, nPos2 + 1)
        Else
            Exit Do
        End If
        nPos1 = InStr(strText, "<")
    Loop
   
    RemoveHTML = strText
End Function

在抓取网页处理源代码要遇到的就是过滤html 提取html中的文字

先有2种方法 一个是使用正则表达式经过测试 用循环的是用正则效率的一万遍以上

Function RemoveHTML( strText )
Set regex = CreateObject("VBScript.RegExp")
RegEx.Pattern = "<[^>]*>"
RegEx.Global = True
RemoveHTML = RegEx.Replace(strText, "")
End Function

还有一种不用正则表达式的

 

Function RemoveHTML(strText)
    Dim nPos1
    Dim nPos2
   
    nPos1 = InStr(strText, "<")
    Do While nPos1 > 0
        nPos2 = InStr(nPos1 + 1, strText, ">")
        If nPos2 > 0 Then
            strText = Left(strText, nPos1 - 1) & Mid(strText, nPos2 + 1)
        Else
            Exit Do
        End If
        nPos1 = InStr(strText, "<")
    Loop
   
    RemoveHTML = strText
End Function