import Foundation
let pattern = #"[A-Za-z](.*)([A-Za-z]|([A-Za-z]{1}[0-9]))(.*?)\s"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
c:\ecpg\BL_Publish_Staging_CFCS_AP_COA_16\COA\fonts\glyphicons-halflings-regular.woff2 20150616 03:56:18 18028
c:\\ecpg\\BL_Publish_Staging_CFCS_PSC_Outage_Notification_16\\images\\LOGO.JPG 20130104 06:16:36 4084
c:\ecpg\myfolder\file with space.cfm 20160803 01:09:54 1574
c:\ecpg\myfolder\no_space.cfm 20160803 01:09:54 1574
c:\ecpg\BL_Publish_Staging_CFCS_PSC_Outage_Notification_16\add_service.cfm 20160803 01:09:54 1574
c:\myfolder\.project 20170221 07:54:10 265
c:\myfolder asdasdasd ASASASDAS 32423423 sadsd a1234 \.project123 20170221 07:54:10 265
"""#
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