import Foundation
let pattern = #"(NUMBER[(](\d{1,3},\d)[)])|(NUMBER[(]((\d{1,3})|(\d{1,3}[,]\d))[)]((?=\/DECIMAL)|(?=\/PERCENTAGE)).*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = #"""
NUMBER(12)
NUMBER(12)/NUMBER
NUMBER(1)
NUMBER(1)/NUMBER
(100)
DATATYPE=NUMBER
NUMBER
NUMBER()
NUMBER(10,2)/PERCENTAGE
NUMBER(12)/DECIMAL
NUMBER(12,4)
NUMBER(13,4)
NUMBER(15)/DECIMAL
NUMBER(15,2)
NUMBER(16)/DECIMAL
NUMBER(10,3)/DECIMAL
NUMBER(3)/DECIMAL
NUMBER(3)/PERCENTAGE
NUMBER(4)/DECIMAL
NUMBER(4,1)
NUMBER(4,2)
NUMBER(5)/DECIMAL
NUMBER(5)/PERCENTAGE
NUMBER(5,2)
NUMBER(5,2)/PERCENTAGE
NUMBER(6)/DECIMAL
NUMBER(7)/DECIMAL
NUMBER(8)/DECIMAL
NUMBER/
NUMBER/CURRENCY
NUMBER/DECIMAL
NUMBER/IFS CURRENCY
NUMBER/INVISIBLE
NUMBER/NUMBER
NUMBER/PERCENTAGE
NUMBER/UPPERCASE
NUMBER=
Number
number
"""#
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