import Foundation
let pattern = #"^(?<parameter>[^:]+)\:\s*(?:(?<value>[A-Za-z]+\:\s*[^>]+))\s*(?<description>.*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
pts_time: PTS: 1741047514> Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972
pts_time: PTS: 1741047514 [0x67C646DA] > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972
private_indicator: '0' reserved: '11'
section_length: 0x39 (57) protocol_version: 0
encrypted_packet: 0 no part of this message is encrypted
encryption_algorithm: 0 No encryption
pts_adjustment: 0xFFFF7F18 (33000) > Time: 95443.4 sec > (hh:mm:ss.ms) 26:30:43.351
cw_index: 0x00 (0) tier: 0x0FFF (4095)
splice_command_length: 0x0005 (5) splice_command_type: 0x06 (6) time_signal [] time_signal: > Time: 19345 sec > (hh:mm:ss.ms) 05:22:24.972
time_specified_flag: 1 presence of the pts_time field
reserved: 0x3F (63) descriptor_loop_length: 35 [] Descriptors: [] segmentation_descriptor (0x02): Content Identification (0x01)
descriptor_tag: 0x02 (2) descriptor_length: 0x21 (33)
identifier: 0x43554549 (CUEI) segmentation_event_id: 0x00000001 (1)
segmentation_event_cancel_indicator: '0' a previously sent segmentation event, identified by segmentation_event_id, has NOT been cancelled
reserved: 0x7F (127) program_segmentation_flag: '1' the message refers to a Program Segmentation Point and that the mode is the Program Segmentation Mode whereby all PIDs/components of the program are to be segmented
segmentation_duration_flag: '0' No presence of segmentation_duration field
delivery_not_restricted_flag: '1' the next five bits are reserved
reserved: 0x1F (31) segmentation_upid_type: 0x01 (1) Deprecated: use type 0x0C; The segmentation_upid does not follow a standard naming scheme.
segmentation_upid_length: 0x12 (18) segmentation_type_id: 0x01 (1) Content Identification
segment_num: 0x01 (1) segments_expected: 0x01 (1)
CRC_32: 0x46D15AF3 CRC OK
// try this one first
// (a) ^(?<parameter>[^:]+)\:\s*(?:(?<value>[A-Za-z]+\:\s*[^>]+))
// (b) ^(?<parameter>[^:]+)\:\s*(?:(?<value>[A-Za-z]+\:\s*[^>]+))\s*(?<description>.*)
// in case of no match of the above one use the second
// (a) ^(?<parameter>[^:]+):\s*(?<value>[\w']+(?:\s*\([^)]*\))*)
// (b) ^(?<parameter>[^:]+):\s*(?<value>[\w']+(?:\s*\([^)]*\))*)\s*(?<description>.*)
// substitution:
// ["$1", "$2", "$3"]
"""#
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