import Foundation
let pattern = #"\(((?:[^()]|(?R))+)\)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"ProToCooL,unknown|DESKTOP-29COFES,10.20.9.53|Hewlett-Packard,179C,PCWJA001X3UHII,KBC Version 42.32|i5-3320M,2.60GHz,2,4,256,3072,U3E1,GenuineIntel|{2,(IDT High Definition Audio CODEC,IDT),(Intel(R) Display Audio,Intel(R) Corporation)}|{2,(Samsung,003355A3,M471B5773DH0-CH9 ,24,2147483648,1333),(Hynix/Hyundai,467CA639,HMT351S6EFR8A-PB ,24,4294967296,1333)}|{1,(IDE,Hitachi HTS725050A7E630, FT51009Y7J61BK,3,733004892,476937.531738281)}|{7,(Send To OneNote 2013,False,Local,Send to Microsoft OneNote 15 Driver),(Microsoft XPS Document Writer,False,Local,Microsoft XPS Document Writer v4),(Microsoft Print to PDF,False,Local,Microsoft Print To PDF),(HP Universal Printing PCL 6,False,Local,HP Universal Printing PCL 6),(HP LaserJet P3010 Series (10.20.9.36),False,Local,HP Universal Printing PCL 6),(Fax,False,Local,Microsoft Shared Fax Driver),(Adobe PDF,True,Local,Adobe PDF Converter)}"#
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