正则匹配 限制字数问题.

moke520 2010-11-11 11:38:11
学习正则中..


引用
Microsoft VBScript Regular Expressions 5.5


Option Explicit

Function TestRegExp(myPattern As String, myString As String)
''Create objects.
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String

'' Create a regular expression object.
Set objRegExp = New RegExp

''Set the pattern by using the Pattern property.
objRegExp.Pattern = myPattern

'' Set Case Insensitivity.
objRegExp.IgnoreCase = True

''Set global applicability.
objRegExp.Global = True

''Test whether the String can be compared.
If (objRegExp.Test(myString) = True) Then

''Get the matches.
Set colMatches = objRegExp.Execute(myString) '' Execute search.

For Each objMatch In colMatches '' Iterate Matches collection.
' RetStr = RetStr & "position "
' RetStr = RetStr & objMatch.FirstIndex & ". Match Value is ''"
RetStr = RetStr & objMatch.Value & "" & vbCrLf
Next
Else
RetStr = "String Matching Failed"
End If
TestRegExp = RetStr
End Function

Private Sub Command1_Click()
Dim strurl As String
Dim xmlobject As Object
Dim strHTML As String
Dim RegStrName As String

strurl = "http://bbs.duowan.com/thread-17923560-3-1.html"

Set xmlobject = CreateObject("Microsoft.XMLHTTP")
xmlobject.Open "GET", strurl, False
xmlobject.Send
If xmlobject.readyState = 4 Then
strHTML = xmlobject.Responsetext
End If
' showMenu(this.id)">名字</a></cite>
RegStrName = (TestRegExp("showMenu+[\S]+</a>", strHTML))
Debug.Print RegStrName
End Sub





Function TestRegExp 是网上复制的. 里面的东西不理解什么意思.

试用这个链接的时候 发现 头一个用户名匹配不到..
∨ 路、  ̄過
里面有个空格还是个什么东西..

然后想用限制字数的方法去取..
(TestRegExp("showMenu+.,{1,9}+</a>", strHTML))
结果报错.

不懂怎么解决. 求教.
...全文
365 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
moke520 2010-11-11
  • 打赏
  • 举报
回复
好吧 - -
我换个别的办法。
homer3382 2010-11-11
  • 打赏
  • 举报
回复

Option Explicit

Function TestRegExp(myPattern As String, myString As String)
''Create objects.
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String

'' Create a regular expression object.
Set objRegExp = New RegExp

''Set the pattern by using the Pattern property.
objRegExp.Pattern = myPattern

'' Set Case Insensitivity.
objRegExp.IgnoreCase = True

''Set global applicability.
objRegExp.Global = True

''Test whether the String can be compared.
If (objRegExp.Test(myString) = True) Then

''Get the matches.
Set colMatches = objRegExp.Execute(myString) '' Execute search.

For Each objMatch In colMatches '' Iterate Matches collection.
' RetStr = RetStr & "position "
' RetStr = RetStr & objMatch.FirstIndex & ". Match Value is ''"
RetStr = RetStr & objMatch.Value & "" & vbCrLf
Next
Else
RetStr = "String Matching Failed"
End If
TestRegExp = RetStr
End Function

Private Sub Command1_Click()
Dim strurl As String
Dim xmlobject As Object
Dim strHTML As String
Dim RegStrName As String

strurl = "http://bbs.duowan.com/thread-17923560-3-1.html"

Set xmlobject = CreateObject("Microsoft.XMLHTTP")
xmlobject.Open "POST", strurl, False
xmlobject.Send
If xmlobject.readyState = 4 Then
strHTML = xmlobject.Responsetext
End If
' showMenu(this.id)">名字</a></cite>

RegStrName = (TestRegExp("showMenu.this.id.+[\S]+</a></cite>", strHTML))
Debug.Print RegStrName
End Sub

想用限制字数的方法去取,这个恐怕不行,因为WEB SERVER响应的返回的网页本身this.id本身就不是一个数字。。。。
赵4老师 2010-11-11
  • 打赏
  • 举报
回复
在字符串扫描问题域中,
正则表达式不是万能的;
有限状态自动机才是万能的。
参考《编译原理》词法分析 有限状态自动机

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧