import Foundation
let pattern = #"(?:(Dim|Public|Private) )(?:(WithEvents) )?(?:(?:(?!Declare)(?!Type)(?!Enum)(?!Const)(\w*))(\(\))?)(?: As )?(?:(New) )?(\w*)(, [\w ,\(\)]*)?"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
Attribute VB_Name = "mdl_WbkInits"
Option Compare Database
Public WithEvents ue_u23 As New String, soumeth_34ing as somethnig, asi As Anthere item, Maybe lastOne
Public ueu As Variant
Dim ueu
Public Declare Function IIDFromString Lib "ole32" (ByVal lpsz As Long, ByRef lpiid As UUID) As Long
Private Declare Function IIDFromString Lib "ole32" (ByVal lpsz As Long, ByRef lpiid As UUID) As Long
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Type UUID 'GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Public Enum e_ControlEventCatch
e_onClick
e_onChange
e_onUpdate
e_onUpdated
e_onDblClick
e_onFocus
e_onExtClick
e_onExtDblClick
e_onActivate
e_onDeActivate
End Enum
Public Const IID_IDispatch As String = "{00020400-0000-0000-C000-000000000046}"
Const OBJID_NATIVEOM As Long = &HFFFFFFF0
"""#
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