import Foundation
let pattern = #"(?>[^;]*;){5}(?>[^,]*,){2}\s*[^,^\s]*\s*([^,^;]*)(?>[^;^\r^\n]*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .allowCommentsAndWhitespace)
let testString = #"2009311179;2723088770;Комсомольское отделение филиала ПАО "ДЭК"-"Хабаровскэнергосбыт";2018-10-23;0de159ee-ebc3-4a1e-81ca-63d8fe5a8558;1093163;Хабаровский край, Солнечный район, п. Хурмули, ул. Школьная, д. 20;;Солнечный;;;421;;Школьная;529;20;;;7;1;16;2012-08-01;65,1;41,3;2012-08-01;3;1;12DEC0701E80601161;;2012-08-01;;Капарушкин;Виталий;Викторович;;;4;4;;2012-08-01"#
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