import Foundation
let pattern = #"[-0-9xX.][-0-9xX\/ .]{2,}"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
matricula SIGRH no 57.477-5
matricula SIAPE no 1411145
matricula no 6048036X
matricula n 53.477-3
matricula 154.489-6
matricula36.134-8
devidamente matriculado sob os no 340.661 e 336.548
matricula GDF 1688857X
matricula GDF 16692 4 11
matricula: 191641-6
matricula GDF no 1.657.641-1
matricula 1.227- 0
matricula/GDF 1.434.238-
matricula 180.397-2, SIAPE 1265862
Matricula no 158930x
matricula n.o 168.804-04
Matricula 15.762/7
matricula SIGRH no 57.413-9
matricula 57.752-9, SIAPE 1411401
matricula: no 28714-2
matricula:0127824-X
matricula JCDF 76/2017
Matricula 024984 01 55 2018 4 00021 086
matricula no 1.690.256-4
Matricula n.o 0.172.827-X
matricula no 1-9
matricula 1.671.636-1
"""#
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