If this is simple, please point me at a link? I have searched, but perhaps don't know how to pinpoint my search. (Syntax is king.)
The basic part of my question is, can I filter an array, using a command like:
newArray = Filter(existingArray, "some matching RegEx string")
So... This may be a stupid question anyway. I'm just curious if a real internal Filter method does exist.
This code appears to work well. I pass it an array. It returns a smaller array of matches.
Function FilterArrayRegEx(arr As Variant, RegExToMatch$) As Variant
Dim regexOne As Object
Dim LocalLine As Variant, Match As Variant, theMatches As Variant, IgnoreCase As Variant
'
Dim stringOne$, strMatches$, arrMatches$()
'
Set regexOne = New RegExp
'
With regexOne
.Pattern = RegExToMatch
' .MultiLine = True ' not relevant in this use
' .Global = True ' not needed for IP addresses
' .IgnoreCase = IgnoreCase ' not needed for IP addresses
End With
'
For Each LocalLine In arr
stringOne = LocalLine
Set theMatches = regexOne.Execute(stringOne)
For Each Match In theMatches
strMatches = IIf(Len(strMatches), strMatches & vbCrLf & LocalLine, LocalLine) ' This only skips a pesky leading vbCrLf
Next
Next
arrMatches = Split(strMatches, vbCrLf)
FilterArrayRegEx = arrMatches
strMatches = vbNullString
Erase arrMatches
End Function
I'm also not certain if I need to dig into the 'Multiline' parameter to simplify my code.
Bookmarks