Ger Cas
Ger Cas

Reputation: 2298

How to save SubMatches as array and print not empty submatches?

When I try the following Regex code and add a "Add Watch" (Shift + F9) to Matches

Sub TestRegEx1()
Dim regex As Object, Matches As Object
Dim str As String

str = "This is text for the submatches"
Set regex = CreateObject("VBScript.RegExp")

regex.Pattern = "Th(is).+(for).+(submatches)|.+(\d)|([A-Z]{3})"
regex.IgnoreCase = True

Set Matches = regex.Execute(str)

End Sub

I see that Matches is structured like this (with 2 empty submatches): enter image description here

2 questions:

How can I save in an array variable the SubMatches?

How can I Debug.Print only elements that are not empty?

I've tried doing like below but is not working

Set Arr = Matches.SubMatches
Set Arr = Matches(1).SubMatches
Set Arr = Matches.Item(1).SubMatches

Thanks in advance

Upvotes: 2

Views: 649

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627146

You may use a Collection and fill it on the go.

Add

Dim m, coll As Collection

Initialize the collection:

Set coll = New Collection

Then, once you get the matches, use

If Matches.Count > 0 Then               ' if there are matches
 For Each m In Matches(0).SubMatches    ' you need the first match submatches
  If Len(m) > 0 Then coll.Add (m)       ' if not 0 length, save value to collection
 Next
End If

Result of the code with changes:

enter image description here

Upvotes: 1

QHarr
QHarr

Reputation: 84465

Is the following what you intended? Oversize an array at the start and redim at the end. First version prints only non-empty but stores all. Second version prints and stores only non-empty.

You probably want to .Test to ensure there are matches.

Option Explicit

Sub TestRegEx1()
    Dim regex As Object, matches As Object, match As Object, subMatch As Variant
    Dim str As String, subMatches(), i As Long
    ReDim subMatches(0 To 1000)

    str = "This is text for the submatches"
    Set regex = CreateObject("VBScript.RegExp")

    regex.Pattern = "Th(is).+(for).+(submatches)|.+(\d)|([A-Z]{3})"
    regex.IgnoreCase = True

    Set matches = regex.Execute(str)
    For Each match In matches
        For Each subMatch In match.subMatches
            subMatches(i) = match.subMatches(i)
            If Not IsEmpty(subMatches(i)) Then Debug.Print subMatches(i)
            i = i + 1
        Next
    Next
    ReDim Preserve subMatches(0 To i)
End Sub

If you only want to store non-empty then

Option Explicit

Sub TestRegEx1()
    Dim regex As Object, matches As Object, match As Object, subMatch As Variant
    Dim str As String, subMatches(), i As Long
    ReDim subMatches(0 To 1000)

    str = "This is text for the submatches"
    Set regex = CreateObject("VBScript.RegExp")

    regex.Pattern = "Th(is).+(for).+(submatches)|.+(\d)|([A-Z]{3})"
    regex.IgnoreCase = True

    Set matches = regex.Execute(str)
    For Each match In matches
        For Each subMatch In match.subMatches
            subMatches(i) = match.subMatches(i)
            If Not IsEmpty(subMatches(i)) Then
                Debug.Print subMatches(i)
                i = i + 1
            End If
        Next
    Next
    ReDim Preserve subMatches(0 To i)
End Sub

Upvotes: 1

Related Questions