import Foundation
let pattern = ##"\b(S5TIME|S5T|TIME|LTIME|T|LT|D|TOD|LTOD|DT|DTL|LDT)#[0-9|+|-]*([^(\s|;)]+)"##
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = ##"""
DT#1990-01-01-00:00:00.000
DT#2089-12-31-23:59:59.999
LDT#1970-01-01-00:00:00.000000000
LDT#2262-04-11-23:47:16.854775807
DTL#1970-01-01-00:00:00.0
DTL#2262-04-11-23:47:16.854775807
LTOD#00:00:00.000000000
LTOD#23:59:59.999999999
TOD#00:00:00.000
TOD#23:59:59.999
D#1990-01-01
D#2169-06-06
LT#-106751d_23h_47m_16s_854ms_775us_808ns
LT#+106751d_23h_47m_16s_854ms_775us_807ns
LT#11350d_20h_25m_14s_830ms_652us_315ns
LTIME#11350d_20h_25m_14s_830ms_652us_315ns;
T#-24d_20h_31m_23s_648ms
T#+24d_20h_31m_23s_647ms
T#10d_20h_30m_20s_630ms
TIME#10d_20h_30m_20s_630ms
S5T#0MS
S5T#2H_46M_30S_0MS
S5T#10s
S5TIME#10s
"""##
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