Converting a method I had in Java to VBA is turning out to be much more difficult than anticipated.
I have a regex which can be found here
It uses named object groups which after some research, do not appear to be supported by VBA. I'm attempting to write a function for excel which will format IP addresses into different formats based on the inputs to the function.
How can I work around using Named Groups in order to capture the different patterns?
Function formatIP(item As String, displayType As String) As String
'displayTypes CIDR,MASK,RANGE
'Set theRegEx = CreateObject("VBScript.RegExp")
Dim theRegEx As New RegExp
With theRegEx
.Global = True
.MultiLine = False
.IgnoreCase = False
.Pattern = "(?<address>\d{1,3}(?:\.\d{1,3}){2}\.(?<FromSeg>\d{1,3}))(?:(?:\/|\s+\/\s+)(?<CIDR>\d{1,2})|(?:-|\s+to\s+)(?<ToSeg>\d{1,3}(?![\d\.]))|(?:-|\s*to\s+)(?<ToIP>\d{1,3}(?:\.\d{1,3}){3})|\s+(?<Mask>25\d(?:\.\d{1,3}){3})|\s*)?"
.Execute (item)
End With
'Set MyMatches = theRegEx.Execute(item)
Debug.Print "SubMatches.Count: " & MyMatches.item(0).SubMatches.Count
If MyMatches.Count <> 0 Then
With MyMatches
For myMatchCt = 0 To .Count - 1
Debug.Print "myMatchCt: " & myMatchCt
For subMtCt = 0 To .item(subMtCt).SubMatches.Count - 1
Debug.Print "subMtCt: " & subMtCt
Debug.Print ("," & .item(myMatchCt).SubMatches.item(subMtCt))
Next
Next
End With
Else
Debug.Print "No Matches"
End If
formatIP = ""
End Function