Home > Software design >  how to output regex group values
how to output regex group values

Time:09-13

I need the group values instead of the matches. This is how i tried to get those:

    Dim item as Variant, matches As Object, match As Object, subMatch As Variant, subMatches(), row As integer

    row = 1

    For Each item In arr

        With regex

            .Pattern = "\bopenfile ([^\s] )|\bopen file ([^\s] )"
            Set matches = regex.Execute(item)

    For Each match In matches
        For Each subMatch In match.subMatches
            subMatches(i) = match.subMatches(i)
            ActiveSheet.Range("A" & row).Value = subMatches(i)
            row = row   1
            i = i   1
        Next subMatch
    Next match
            
        End With
            
    Next item

This is the text from where it should be extracted:

Some help would be great :)

Open File file.M_p3_23432e done
Openfile file.M_p4_6432e done
Open File file.M_p3_857432 done
Open File file.M_p4_34892f done
Openfile file.M_p3_781 done

Info: I'm using Excel VBA.. If that is important to know.

CodePudding user response:

You can revamp the regex to match and capture with one capturing group:

\bopen\s?file\s (\S )

See the regex demo.

Details:

  • \b - word boundary
  • open - a fixed word
  • \s? - an optional whitespace
  • file - a fixed word
  • \s - one or more whitespaces
  • (\S ) - Group 1: one or more non-whitespaces.

Now, the file names are always in SubMatches(0).

Note that the regex must be compiled with the case insensitive option and global (if the string contains multiple matches):

With regex
    .Pattern = "\bopen\s?file\s (\S )"
    .IgnoreCase = True
    .Global = True
End With
  • Related