2015-10-04 44 views
1

我有一個模式,我發現一個匹配。我應該如何處理的情況下,其他的比賽我有多個選擇Vbs的正則表達式如何處理多個匹配

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」,但我怎麼能得到其他人呢?

回答

0

在結束與next的循環之前,您需要先處理result

當前,您將每個結果分配給result,但是會立即結束循環並轉到下一個結果。

+0

maybeWeCouldStealAVan,Thank you for your comment。您能否提供一個可行的示例 – Alex

+0

@Alex您可以自己嘗試一下嗎? – Lankymart

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 
+0

omegastripes,非常感謝您的幫助 – Alex