Vbs正则表达式如何处理多个匹配

时间:2015-10-04 08:11:11

标签: regex vbscript

我有一个模式,我找到一个匹配。如果我有多个选择,我应该如何处理其他比赛

    Dim re, targetString, colMatch, objMatch
Set re = New regexp
With re
  .pattern = ">([\s,\S]*?)<" 
  .Global = True
  .IgnoreCase = True
  .Multiline = True
End With
targetString = ">test and test<  >test2 and test2<   >test3 and test3<"
Set colMatch = re.Execute(targetString)
For Each objMatch In colMatch
result = objMatch.SubMatches.Item(0)
Next

目前我只获得“test3和test3”,但我怎样才能得到其他人?

2 个答案:

答案 0 :(得分:1)

考虑以下示例。它显示了如何将所有子匹配放入数组。

Dim strSourceString, objMatch, arrResults
strSourceString = ">test and test<  >test2 and test2<   >test3 and test3<"
Set objList = CreateObject("Scripting.Dictionary")
With New RegExp
    .Pattern = ">([\s,\S]*?)<"
    .Global = True
    .IgnoreCase = True
    .MultiLine = True
    For Each objMatch In .Execute(strSourceString)
        objList(objList.Count) = objMatch.SubMatches.Item(0)
    Next
End With
arrResults = objList.Items
Set objList = Nothing
MsgBox Join(arrResults, "; ") ' array contains submatches

答案 1 :(得分:0)

在使用result结束循环之前,您需要处理next

目前,您将每个结果分配给result,但随后立即结束循环并继续下一个结果。