首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

正则用于vb读取网页的有关问题

2012-01-26 
正则用于vb读取网页的问题有这么一段代码:有用正则表达式匹配一个html标记: (.*) .* \/\1这个怎么

正则用于vb读取网页的问题
有这么一段代码:有用正则表达式匹配一个html标记: " <(.*)> .* <\/\1> "这个怎么用于匹配标记语言: <tr     class= "hover "   bgcolor= "#f5deb3 "   >
<td   width= "5% "   class= "tdH "> 1 </td>
<td   width= "60% "   class= "tdl "> 消息内容!! </td>
<td   width= "5% "   class= "tdH "> - </td>
<td   width= "5% "   class= "tdH "> ok </td>
<td   width= "5% "   class= "tdH "> - </td>
<td   width= "5% "   class= "tdH "> 有效 </td>
<td   width= "15% "   class= "tdH ">
<a   href= "?edit=15206&this_language=simplified&fr_show=1 "> 修改 </a>
/   <a   href= "?act=0&annix=15206&this_language=simplified&fr_show=1 "> 停用 </a>
/   <a   href= "?alert=15206&this_language=simplified&fr_show=1 "> 戒备 </a>
/   <a   href= "?del=15206&this_language=simplified&fr_show=1 "> 删除 </a>
</td>
<!--   [DTS   460]   -   01/03/2006   (Teh)   Add   on   a   checkbox   for   marquee   function   START   -->
<td>
<input   type= "checkbox "   name= "fr_checker_15206 "   onclick= "marqueeShow( '15206 ') "   >
</td>
<!--   [DTS   460]   -   01/03/2006   (Teh)   Add   on   a   checkbox   for   marquee   function   END   -->
</tr> 中的 <td   width= "60% "   class= "tdl "> 消息内容!! </td> 和 <input   type= "checkbox "   name= "fr_checker_15206 "   onclick= "marqueeShow( '15206 ') "   > ,也就是说我通过匹配提取该html语言中的:“消息内容”“15206”,用vb语言的
而我在正则写的代码是也就是匹配代码:
        Dim   MyReg   As   RegExp
        Dim   MyReg1   As   RegExp
        Dim   objTemp     As   MatchCollection                
        Dim   i   As   Integer
        Set   MyReg1   =   New   RegExp        
        MyReg1.IgnoreCase   =   True
        MyReg1.Global   =   True
        MyReg1.MultiLine   =   True  
        Set   MyReg   =   New   RegExp        
        MyReg.IgnoreCase   =   True
        MyReg.Global   =   True
        MyReg.MultiLine   =   True        
        MyReg.Pattern   =   " <td   width=\ "60%\ "   class=\ "tdl\ "> (.*) <\/td> "和
        MyReg.Pattern   =   " <input   type=\ "checkbox\ "   name=\ "fr_checker_(\d+)\ "   onclick=\ "marqueeShow\(\ '(\d+)\ '\)\ "   > "
但鼠标一离开就提示缺少语句错误,在正则里是可以这样匹配的,但用在vb程序中就出现错误提示,以前用正则匹配函数在vb中用这样的格式可以,但这个却不行啊,麻烦各位高手指点,小弟感激不尽,解决立即给分,谢谢


------解决方案--------------------


//鼠标一离开就提示缺少语句错误

字符串的问题

将MyReg.Pattern = " <td width=\ "60%\ " class=\ "tdl\ "> (.*) <\/td> "
修改为:
MyReg.Pattern = " <td width=\ " "60%\ " " class=\ " "tdl\ " "> (.*) <\/td> "
其余类似,简单的说,就是字串中的一个 ",要用两个 "代替
[解决办法]
首先建议楼主先把这两个字符串提取出来,


Function func_RegExpTest1(patrn, strng) As Boolean '正ze表da式匹配操作(?找个数)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
For Each Match In Matches
str1 = str1 & Match.Value
Next
MsgBox str1
End Function
然后:
Function func_ReplaceTest(patrn, replStr, str As String) '正ze表da式匹配操作(替?)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
regEx.Execute (str)
str2 = regEx.Replace(str, replStr)
func_ReplaceTest = True
End Function

str = " <td width= " "60% " " class= " "tdl " "> ([^> ]*) </td> | <input type= " "checkbox " " name= " "fr_checker_(\d+) " " "
call func_RegExpTest1(str, "楼主的html文本 ")
Call func_ReplaceTest(str, "$1$2 ", str1)
msgbox str2

热点排行