import Foundation
let pattern = #"\.-?[_a-zA-Z]+[_a-zA-Z0-9-]*\s*\{(.*?)}"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
<body> <style type="text/css"> .csB51708B8{text-align:left;text-indent:0pt;margin:0pt 0pt 12pt 0pt;line-height:16.5pt} .csBBD4C4C1{color:#2B2E2F;background-color:transparent;font-family:'Lucida Sans Unicode';font-size:10.5pt;font-weight:normal;font-style:normal;} .csFC0FFCA7{text-align:left;margin:8pt 0pt 12pt 0pt;line-height:16.5pt;list-style-type:decimal;color:#2B2E2F;background-color:transparent;font-family:'Lucida Sans Unicode';font-size:10.5pt;font-weight:normal;font-style:normal} .csB4C51EC2{color:#2B2E2F;background-color:transparent;font-family:'Lucida Sans Unicode';font-size:10.5pt;font-weight:normal;font-style:normal;text-decoration: none;} .csC575EB8B{color:#0000FF;background-color:transparent;font-family:'Lucida Sans Unicode';font-size:10.5pt;font-weight:normal;font-style:normal;text-decoration: underline;} .cs67EAC646{text-align:left;margin:8pt 0pt 8pt 0pt;line-height:16.5pt;list-style-type:decimal;color:#2B2E2F;background-color:transparent;font-family:'Lucida Sans Unicode';font-size:10.5pt;font-weight:normal;font-style:normal} .cs896AFE57{text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt;line-height:16.5pt} .csE2621F37{text-align:left;text-indent:0pt;margin:11pt 0pt 11pt 0pt;line-height:16.5pt} .cs2654AE3A{text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt} .csC8F6D76{color:#000000;background-color:transparent;font-family:Calibri;font-size:11pt;font-weight:normal;font-style:normal;} .cs8D3F54E5{color:#000000;background-color:transparent;font-family:Verdana;font-size:8pt;font-weight:normal;font-style:normal;} </style> <p class="csB51708B8"><span class="csBBD4C4C1">
...
</body>
"""##
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