import Foundation
let pattern = #"(Dim|Static)(?: )([\S]*)(\(\))?(?: )?(As)? ?(New)? ?([^\r\t\f\v,]*)(,[\S\s][^\n]*)?"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
On Error GoTo Err
Dim obj_AccessApp As Object, Test As xxxy, euaou sai xxx
Set obj_AccessApp = Nothing
Set db_Ref = Nothing
Dim hWndMain As LongPtr
hWndMain = FindWindowEx(0&, 0&, "OMAIN", vbNullString)
Do While hWndMain <> 0
Call z_mGetObjectFromHwnd(hWndMain, obj_AccessApp)
Dim str_DbName As String
str_DbName = ""
On Error Resume Next
str_DbName = obj_AccessApp.CurrentDb.Name
On Error GoTo Err
If str_DbName Like "*\PPMSupportDb.accdb" Then
On Error Resume Next
obj_AccessApp.CloseCurrentDatabase
obj_AccessApp.Quit
Set obj_AccessApp = Nothing
On Error GoTo 0
Debug.Print "Support item killed"
End If
If str_DbName = "" Then
On Error Resume Next
obj_AccessApp.Quit
Set obj_AccessApp = Nothing
On Error GoTo Err
Debug.Print "Blank item killed"
End If
hWndMain = FindWindowEx(0&, hWndMain, "OMAIN", vbNullString)
Loop
Exit_Default:
Set obj_AccessApp = Nothing
Exit Sub
Err:
MsgBox "GetDatabaseNames" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
Resume Exit_Default
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression