import Foundation
let pattern = #"(?s)(\d+)\n +(.*?)\s*(?=\d+\n)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
7. On 6 March 2013, the Appeals Chamber filed the Decision on Victim
Participation, in which it decided that the victims “may, through their legal
1
The full citation, including the ICC registration reference of all designations and abbreviations used in
this judgment are included in Annex 1.
2
A more detailed procedural history is set out in Annex 2 of this judgment.
ICC-01/04-02/12-271-Corr 07-04-2015 7/117 EK A
8/117
representatives, participate in the present appeal proceedings for the purpose of
presenting their views and concerns in respect of their personal interests in the issues
on appeal”.
3
8. On 19 March 2013, the Prosecutor filed, confidentially, ex parte, available to the
Prosecutor and Mr Ngudjolo only, the Document in Support of the Appeal. The
Prosecutor filed a confidential redacted version of the Document in Support of the
Appeal on 22 March 2013, and a public redacted version of the Document in Support
of the Appeal on 3 April 2013. In the redacted version of the Document in Support of
the Appeal, the Prosecutor’s entire third ground of appeal was redacted.
"""#
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