Alex
Alex

Reputation: 295

Vbs regex how to deal with multiple matches

I have a pattern with which I find one match. How should I deal with the other matches in case I have multiple choices

    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

At the present I am getting only "test3 and test3", but how can I get the others?

Upvotes: 2

Views: 1465

Answers (2)

omegastripes
omegastripes

Reputation: 12612

Consider the example below. It shows how to put all submatches into array.

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

Upvotes: 1

maybeWeCouldStealAVan
maybeWeCouldStealAVan

Reputation: 15610

You'll need to process result before you end the loop with next.

Currently, you assign each of the results to result, but then immediately end the loop and move on to the next result.

Upvotes: 0

Related Questions