import Foundation
let pattern = #"({[^}]+})+, -- (.+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
{ spell = 126, type = "ability"}, -- Eye of Kilrogg
{ spell = 172, type = "ability", requiresTarget = true, debuff = true}, -- Corruption
{ spell = 686, type = "ability", requiresTarget = true}, -- Shadow Bolt
{ spell = 698, type = "ability"}, -- Ritual of Summoning
{ spell = 702, type = "ability", requiresTarget = true, debuff = true}, -- Curse of Weakness
{ spell = 710, type = "ability", requiresTarget = true, debuff = true}, -- Banish
{ spell = 755, type = "ability"}, -- Health Funnel
{ spell = 980, type = "ability", requiresTarget = true, debuff = true}, -- Agony
{ spell = 1714, type = "ability", requiresTarget = true, debuff = true}, -- Curse of Tongues
{ spell = 3110, type = "ability", requiresTarget = true}, -- Firebolt
{ spell = 3716, type = "ability", requiresTarget = true}, -- Consuming Shadows
{ spell = 5484, type = "ability"}, -- Howl of Terror
{ spell = 5782, type = "ability", requiresTarget = true, debuff = true}, -- Fear
{ spell = 6358, type = "ability", requiresTarget = true}, -- Seduction
{ spell = 6360, type = "ability", requiresTarget = true}, -- Whiplash
{ spell = 6789, type = "ability", requiresTarget = true, talent = 14 }, -- Mortal Coil
"""#
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