开发者

vbscript regular expression into an array

With the code belo开发者_JAVA技巧w I am trying to pull each url I extract using the regular expression into an array that I can call later along with the count of urls. Not sure how to grab all of them.

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


I tested this with a fake string, your regexp results seemed a bit wonky so I changed it (grabbed from here). Results of the 1st match (you capture 2?) are placed in the matches array:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜