import Foundation
let pattern = #"(?m)(?:^recipeingredient:\s\[\".*|\G)[^\"]*\K\\"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
description: "Easiest Way to Prepare Speedy Dal Chicken"
title: "How to Prepare Award-winning Dal Chicken"
date: 2020-12-10T07:38:14.079Z
image: https://img-\global.cpcdn.com/recipes/f92bd4e2e07d9fa1/751x532cq70/dal-chicken-recipe-main-photo.jpg
thumbnail: https://img-global.cpcdn.com/recipes/f92bd4e2e07d9fa1/751x532cq70/dal-chicken-recipe-main-photo.jpg
cover: https://img-global.cpcdn.com/recipes/f92bd4e2e07d9fa1/751x532cq70/dal-chicken-recipe-main-photo.jpg
author: Mathilda Day
ratingvalue: 4.9
reviewcount: 44224
recipeingredient: ["1 chicken breast - cut in small pieces","1 \ 1/2 cup chana dal - soaked over night and partially boiled","1 tblsp red chilli powder","1 tblsp dhaniya powder","pinch turmeric powder","1 1/2 tblsp of ginger-garlic paste"," Salt per taste","3 black peppers (whole)","3,4 cloves","1 cinnamon stick","1/2 tsp cumin seeds","1 medium onion - finely chopped","1 medium tomato paste","4 tblsp oil","as needed Water",]
recipeinstructions: ["Heat oil in a wok - Add cumin seeds, black peppers, cloves, and cinnamon stick.","Add onions.Let it cook till it turns light brownAdd tomato paste.Let it cook till oil separates.","Add lal masala (Step 3) & salt - Cook till oil separates and the masala is cooked.Add chicken and chana dal.Add water as needed.","Cook till chicken and the dal has properly cooked and the gravy is at the consistency that you want.","Serve with roti aur chapati.",]
categories:
"""#
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