import Foundation
let pattern = #"(S\w.*\d[:])|(([0-9.]+|(-[0-9.]+)))|([A-Z]{2}\b)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
ACTIONS:
[ { LONGITUDES:
{ SHUTTLE_LON_A6: [ 748, [ 'CI', 76.5, 980.5 ] ],
SHUTTLE_LON_A5:
[ 271,
[ 'CL', 0, 76.5 ],
[ 'CI', 76.5, 980.5 ],
[ 'CL', 980.5, 1057 ] ],
SHUTTLE_LON_A4: [ 129 ],
SHUTTLE_LON_A3: [ 107 ],
SHUTTLE_LON_A2: [ -35 ],
SHUTTLE_LON_A1: [ -57 ] },
LATITUDES: { SHUTTLE_LAT_1: [ [ 1057 ] ] } },
{ LONGITUDES:
{ SHUTTLE_LON_A6: [ 978, [ 'CL', 76.5, 980.5 ] ],
SHUTTLE_LON_A5:
[ 727,
[ 'CL', 0, 76.5 ],
[ 'CI', 76.5, 980.5 ],
[ 'CL', 980.5, 1057 ] ],
SHUTTLE_LON_A4: [ 250, [ 'CI', 76.5, 980.5 ] ],
SHUTTLE_LON_A3: [ 20, [ 'CL', 76.5, 980.5 ] ],
SHUTTLE_LON_A2: [ -122 ],
SHUTTLE_LON_A1: [ -144 ] },
LATITUDES:
{ SHUTTLE_LAT_1:
[ [ 20.5, [ 'CI', 0, 1100 ] ],
[ 34.5, [ 'CI', 0, 1100 ] ],
[ 58.5, [ 'CI', 0, 1100 ] ],
[ 76.5,
[ 'CL', 0, 271 ],
[ 'CI', 271, 727 ],
[ 'CL', 727, 1100 ] ],
[ 980.5,
[ 'CL', 0, 271 ],
[ 'CI', 271, 727 ],
[ 'CL', 727, 1100 ] ],
[ 998.5, [ 'CI', 0, 1100 ] ],
[ 1022.5, [ 'CI', 0, 1100 ] ],
[ 1036.5, [ 'CI', 0, 1100 ] ],
[ 1057, [ 'CL', 0, 1100 ] ],
[ 1057 ] ] } } ] } ]
"""#
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