我尝试使用正则表达式匹配文本中的模式,但出现此运行时错误。你能帮我解决这个问题吗?
运行时错误 91:对象变量或未设置块变量
代码:
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim Name As String
Dim regexp As Object
Dim colregmatch As MatchCollection
Name = "D:/test_DC.txt"
Set ts = fso.OpenTextFile(Name, ForReading)
Do While Not ts.AtEndOfStream
regexp.Pattern = "KENNFELD\s+([ 0-9]*)" //Error
Set colregmatch = regexp.Execute(searchstr)
If colregmatch.Count <> 0 Then
For Each Match In colregmatch
MsgBox Match
Next Match
End If
Loop
更新:
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim Name As String
Dim regx As New regexp
Dim colregmatch As MatchCollection
Name = "D:/test_DC.txt"
'Set regexp = CreateObject("vbscript.regexp")
Set ts = fso.OpenTextFile(Name, ForReading)
Do While Not ts.AtEndOfStream
regx.Pattern = "KENNFELD\s+([ 0-9]*)"
Set colregmatch = regx.Execute(searchstr)
If colregmatch.Count <> 0 Then
For Each Match In colregmatch
MsgBox Match
Next Match
End If
Loop