import Foundation
let pattern = #"^(<\!\-\- wp:onecms\/recipe-time \{\"type\"\:\"cook\")\S(.*[a-z\:0-9\}\s])(\/\-\->)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
<!-- wp:onecms/recipe-header {"dek":"\u003cp\u003eHungry for some \u003ca href=\u0022https://www.southernliving.com/recipes/old-fashioned-apple-pie\u0022\u003eapple pie\u003c/a\u003e? We are right there with you. Here's the recipe from the Whispering Canyon Café at Disney’s Wilderness Lodge—dessert will never be the same.\u003c/p\u003e"} /-->
<!-- wp:onecms/primary-media /-->
<!-- wp:onecms/recipe-media /-->
<!-- wp:onecms/recipe-container -->
<!-- wp:onecms/recipe-details -->
<!-- wp:onecms/recipe-metadata {"yieldDescription":"Makes one 9-inch pie."} /-->
<!-- wp:onecms/recipe-times -->
<!-- wp:onecms/recipe-time {"type":"hands-on","days":0,"hours":0,"minutes":45} /-->
<!-- wp:onecms/recipe-time {"type":"cook","days":0,"hours":1,"minutes":0} /-->
<!-- /wp:onecms/recipe-times -->
<!-- /wp:onecms/recipe-details -->
<!-- wp:onecms/recipe-ingredients -->
<!-- wp:onecms/recipe-section-header {"content":"Pie Crust"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"butter, cut into small pieces","customMeasure":"tablespoons","customQuantity":"4"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"shortening","customMeasure":"cup","customQuantity":"0.25"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"all-purpose flour","customMeasure":"cups","customQuantity":"1.75"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"sugar","customMeasure":"teaspoons","customQuantity":"4"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"coarse salt","customMeasure":"teaspoon","customQuantity":"0.13"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"2% milk","customMeasure":"cup","customQuantity":"0.33"} /-->
<!-- wp:onecms/recipe-section-header {"content":"Apples"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"water","customMeasure":"cup","customQuantity":"1"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"Granny Smith apples, peeled and sliced","customQuantity":"6"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"apple pie spice","customMeasure":"teaspoon","customQuantity":"2"} /-->
<!-- wp:onecms/recipe-section-header {"content":"Apple Pie Batter"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"butter, softened","customMeasure":"cup","customQuantity":"0.5"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"sugar","customMeasure":"cup","customQuantity":"0.67"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"eggs","customQuantity":"2"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"flour","customMeasure":"cups","customQuantity":"1.5"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"baking powder","customMeasure":"teaspoon","customQuantity":"1.5"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"salt","customMeasure":"teaspoon","customQuantity":"0.13"} /-->
<!-- wp:onecms/recipe-ingredient-custom {"customIngredient":"heavy cream","customMeasure":"cup","customQuantity":"0.25"} /-->
<!-- /wp:onecms/recipe-ingredients -->
<!-- wp:onecms/recipe-nutrition {"displayNutritionRecipe":false} /-->
<!-- wp:onecms/recipe-directions -->
<!-- wp:onecms/recipe-direction {"directionId":"ddaa1eec-7bec-4284-b4e3-53b35cb83941","content":"\u003cstrong\u003eFOR PIE CRUST: \u003c/strong\u003e"} /-->
<!-- wp:onecms/recipe-direction {"directionId":"e546e3c3-96a5-4be5-94fa-34ee625f3a48","content":"\u003cstrong\u003eFOR APPLES:\u003c/strong\u003e"} /-->
<!-- wp:onecms/recipe-direction {"directionId":"49fbc0c6-12f0-420c-9dc6-19210620e023","content":"\u003cstrong\u003eFOR APPLE PIE BATTER:\u003c/strong\u003e"} /-->
<!-- wp:onecms/recipe-direction {"directionId":"208717df-e3e3-43e9-a2bc-35ad854ae43c","content":"\u003cstrong\u003eFOR APPLE PIE:\u003c/strong\u003e"} /-->
<!-- /wp:onecms/recipe-directions -->
<!-- wp:onecms/recipe-notes -->
<!-- wp:onecms/recipe-note /-->
<!-- /wp:onecms/recipe-notes -->
<!-- /wp:onecms/recipe-container -->
"""#
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