import Foundation
let pattern = #"^([a-zA-Z]+)\s*\{(.*?)}(?=\n[a-zA-Z]+\s*{\n|\Z)"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .dotMatchesLineSeparators])
let testString = #"""
BackdropNode {
inputs 0
name BackdropNode6
tile_color 0x555555ff
label RDN
note_font_size 42
xpos -1136
ypos -4272
bdwidth 451
bdheight 529
}
Write {
file "\[value project_directory]/_output_/\[string range \[file tail \[value root.name]] 0 20].mov"
colorspace sRGB
raw true
file_type mov\{(.*?)\}
mov64_format "mov (QuickTime / MOV)"
mov64_codec AVdh
mov64_dnxhd_codec_profile "DNxHD 422 8-bit 145Mbit"
mov64_dnxhr_codec_profile "SQ 4:2:2 8-bit"
mov64_pixel_format {{0} "yuv420p\tYCbCr 4:2:0 8-bit"}
mov64_quality High
mov64_advanced 1
mov64_write_timecode true
mov64_gop_size 12
mov64_b_frames 0
mov64_bitrate 20000
mov64_bitrate_tolerance 40000000
mov64_quality_min 2
mov64_quality_max 31
render_order 3
checkHashOnRead false
version 132
in_colorspace scene_linear
out_colorspace scene_linear
name Write2
xpos -1025
ypos 2230
}
"""#
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