import Foundation
let pattern = #"modified:\s+((?>\w+\/?)+)[^)\v]*\)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
modified: apps/aaaaaa/bbbbbb/ccccff
modified: apps/ami (new commits)
modified: apps/assess (new commits)
modified: apps/ees121 (new commits)
modified: apps/energy_bourse (new commits)
modified: apps/gis (new commits)
modified: apps/hse (new commits)
modified: apps/aa/aaa/a/bb/b/bb/bc/c22/s/df/s/
modified: apps/management (new commits)
modified: apps/payesh (new commits)
modified: external_apps (modified content)
modified: modules/esb_server (new commits)
modified: modules/formbuilder (new commits, modified content)
modified: modules/reporting (new commits)
modified: modules/safir (new commits)
modified: modules/workflow (new commits)
modified: vendor/raya_framework/client (new commits)
modified: vendor/raya_framework/core (new commits)
"""#
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