将vbscript正则表达式转换为数组

时间:2011-05-10 23:16:07

标签: regex arrays vbscript

使用下面的代码,我试图将我使用正则表达式提取的每个URL拉到一个数组中,我稍后可以调用它与url的数量。不知道如何抓住所有这些。

Set objxmlHTTP = CreateObject("Microsoft.XMLHTTP")
Call objxmlHTTP.open("GET", "website", False)
objxmlHTTP.Send()

strHTML = objxmlHTTP.ResponseText

Dim objRegExp
Set objRegExp = New RegExp

objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "<a\s+href=""(http://.*?)""[^>]+>(\s*\n|.+?\s*)</a>"

Dim objMatch
For Each objMatch in objRegExp.Execute(strHTML)
  objMatch.SubMatches(0)
Next

Set objxmlHTTP = Nothing

1 个答案:

答案 0 :(得分:1)

我用假字符串测试了这个,你的正则表达式结果看起来有点不稳定所以我改了它(从here抓起来)。第一场比赛的结果(你捕获2?)被放置在匹配数组中:

Dim objRegExp
Set objRegExp = New RegExp

objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = ""((https?:\/\/|www.)([-\w.]+)+(:\d+)?(\/([\w\/_.]*(\?\S+)?)?)?)""

dim matches()
dim i: i = 0

Dim objMatch
For Each objMatch in objRegExp.Execute(strHTML)
   redim preserve matches(i)
   matches(i) = objMatch.SubMatches(0)
   i = (i + 1)
Next

Set objxmlHTTP = Nothing

'//read back
for i = 0 to ubound(matches)
   wscript.echo matches(i)
next