import Foundation
let pattern = #" SG_ (\w+) : (\d+)\|(\d+)@[01][+-] \(([0-9E\-\+\.]*),([0-9E\-\+\.]*)\) \[[0-9E\-\+\.]*\|[0-9E\-\+\.]*\] "([^"]*)" Vector__XXX"#
let regex = try! NSRegularExpression(pattern: pattern, options: .dotMatchesLineSeparators)
let testString = #"""
VERSION ""
NS_ :
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
BS_:
BU_:
BO_ 777 VBOX_9: 8 Vector__XXX
SG_ Longitude_48bit : 7|48@0- (1E-07,0) [0|0] "minutes" Vector__XXX
BO_ 776 VBOX_8: 8 Vector__XXX
SG_ Latitude_48bit : 7|48@0- (1E-07,0) [0|0] "minutes" Vector__XXX
SG_ Kalman_filter_status : 56|1@0+ (1,0) [0|1] "" Vector__XXX
BO_ 774 VBOX_6: 8 Vector__XXX
SG_ Vel_Quality : 7|16@0- (0.01,0) [0|0] "km/h" Vector__XXX
BO_ 773 VBOX_5: 8 Vector__XXX
SG_ Trig_Vel_Mph : 55|16@0+ (0.0115078,0) [0|754.163673] "Mph" Vector__XXX
SG_ Trig_Vel_Kmh : 55|16@0+ (0.01852,0) [0|1213.7082] "Km/h" Vector__XXX
SG_ Trig_time : 39|16@0+ (0.01,0) [0|655.35] "Secs" Vector__XXX
SG_ Distance : 7|32@0+ (7.8125E-05,0) [0|335544] "Metres" Vector__XXX
BO_ 772 VBOX_4: 8 Vector__XXX
SG_ Lateral_acceleration : 55|16@0- (0.01,0) [-327.68|327.67] "G" Vector__XXX
SG_ Longitudinal_acceleration : 39|16@0- (0.01,0) [-327.68|327.67] "G" Vector__XXX
SG_ Trig_Dist : 7|32@0+ (7.8125E-05,0) [0|335544] "Metres" Vector__XXX
BO_ 771 VBOX_3: 8 Vector__XXX
SG_ DGPS : 61|1@0+ (1,0) [0|0] "On" Vector__XXX
SG_ Brake_trigger : 60|1@0+ (1,0) [0|0] "On" Vector__XXX
SG_ Vertical_Velocity : 31|16@0- (0.01,0) [0|655.36] "m/s" Vector__XXX
SG_ Altitude_WGS84 : 7|24@0- (0.01,0) [0|42949700] "metres" Vector__XXX
BO_ 770 VBOX_2: 8 Vector__XXX
SG_ Velocity_Mph : 39|16@0+ (0.0115078,0) [0|754.163673] "Mph" Vector__XXX
SG_ Heading : 55|16@0+ (0.01,0) [0|359.99] "Degrees" Vector__XXX
SG_ Velocity_Kmh : 39|16@0+ (0.01852,0) [0|1213.7082] "Km/h" Vector__XXX
SG_ Longitude : 7|32@0- (1E-05,0) [-21474.83648|21474.83647] "Minutes" Vector__XXX
BO_ 769 VBOX_1: 8 Vector__XXX
SG_ Latitude : 39|32@0- (1E-05,0) [-21474.83648|21474.83647] "Minutes" Vector__XXX
SG_ Time_Since_Midnight : 15|24@0+ (0.01,0) [0|0] "Seconds" Vector__XXX
SG_ Sats : 7|8@0+ (1,0) [0|12] "Sats" Vector__XXX
"""#
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