import Foundation
let pattern = #"\"Comment\": \"(.*?)\""#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"[{"Comment": "Tbh the chair should\u2019ve had 4 pistons so Michael could finally eat at the table", "Likes": "4.4K", "Time": "2 years ago", "UserLink": "https://www.youtube.com/channel/UC_aU3uhZDInQSUUosL2uQ9A", "user": "Nates"}, {"Comment": "I want to believe that this isn't a screen personality and he is just a genuine sociopath", "Likes": "4.3K", "Time": "7 months ago", "UserLink": "https://www.youtube.com/channel/UCQBxoqlTxL2YNIm_p6FAEfw", "user": "tank head"}, {"Comment": "Can you build a chair that when you say \u201cI\u2019d like to make an announcement.\u201d Pistons push the chair up to make you taller than everyone else", "Likes": "1.9K", "Time": "7 months ago", "UserLink": "https://www.youtube.com/channel/UC3HyXTP6aLjcZp88iOuytpg", "user": "Kitsune"}, {"Comment": "the tazer desk could easily be converted to a torture bed", "Likes": "245", "Time": "3 months ago", "UserLink": "https://www.youtube.com/channel/UCySFNMcZJm4MWAfiaIRpqvg", "user": "Cpt_Azure"}, {"Comment": "I want to imagine that his friends are just locked up in a house threatened hostage and forced to test his robot inventions", "Likes": "1.8K", "Time": "1 year ago", "UserLink": "https://www.youtube.com/channel/UCHZ6qYCGHm44ge2OGGazSVw", "user": "W"}, {"Comment": "\"I spent 12 hours making this...\"And 11 months tracking a goldfish in a cage...", "Likes": "492", "Time": "3 months ago", "UserLink": "https://www.youtube.com/channel/UCpNND06DunvPMGrSQqjjM_Q", "user": "Joshbobosh"}, {"Comment": "He should've added a delay to the chair so people get lured into a false sense of security", "Likes": "4.7K", "Time": "1 year ago", "UserLink": "https://www.youtube.com/channel/UCESk3Y2OfMMIgfATOsujI6Q", "user": "AshIsHere"}, {"Comment": "It's funny how Yvonne says \"Is that an electric chair?!\", And then he goes and builds one like a year later", "Likes": "3K", "Time": "11 months ago", "UserLink": "https://www.youtube.com/channel/UCol-DCuwRT0sS98uBu1oCBA", "user": "MrMoo1452"}, {"Comment": "\u201cThat is a fuCKING alarm clock.\u201dI love that quote so much", "Likes": "919", "Time": "6 months ago", "UserLink": "https://www.youtube.com/channel/UCuVPAzq8ORnoK8CcEDpTmHA", "user": "Bababooey"}, {"Comment": "I had an art teacher who said, \"Don't lean on the table!\" She was very angry when people did, and I'm assuming she would actually use the tazer table.", "Likes": "79", "Time": "5 months ago", "UserLink": "https://www.youtube.com/channel/UCCzciQr3PjMlYqbMmyjtgDg", "user": "Tom Kas"}, {"Comment": "Make your windshield wipers, automatically wipe in the beat of the music that is played in the car.", "Likes": "3.4K", "Time": "1 year ago", "UserLink": "https://www.youtube.com/channel/UCUfdOys12cHo7waaWkjG4mA", "user": "Benni B."}, {"Comment": "Make a robot that automatically \u201cpermanently removes\u201d people who suggest an idea for a robot", "Likes": "84", "Time": "3 months ago", "UserLink": "https://www.youtube.com/channel/UCOh33A3iLMokwaUXI0Y6e6g", "user": "Swish_Cheetah"}, {"Comment": "I like that Michael calls us his \"worst, most hated enemies\".\nYou can really feel the love.", "Likes": "900", "Time": "2 years ago", "UserLink": "https://www.youtube.com/channel/UCeOSKFvurM-glO4TW8SX3EA", "user": "Camera Lizard"}, {"Comment": "\"Make a robot that poops cereal\"shredded\"Make a robot that piss beer\"genius", "Likes": "361", "Time": "11 months ago", "UserLink": "https://www.youtube.com/channel/UCbSIk0yT8w-sVCI_wNsQkPA", "user": "Ghedo"}, {"Comment": "ya gotta love the fact that the taser desk was acting as an EMP that caused his monitor to shut off several times", "Likes": "626", "Time": "10 months ago", "UserLink": "https://www.youtube.com/channel/UCmqGYYVwZ0nrd5tIxazWddA", "user": "Jackie"}, {"Comment": "I like that the Taser Desk incorporates so much high voltage garbage that it\u2019s making the electronics in his computer spaz out lmao. Who doesn\u2019t love a bad homemade EMP", "Likes": "4", "Time": "12 days ago", "UserLink": "https://www.youtube.com/channel/UCfvtdRAITOtzjZIgoKmCK5w", "user": "SilentGuy"}, {"Comment": "The fact everyone fears Michael is just hilarious", "Likes": "224", "Time": "7 months ago", "UserLink": "https://www.youtube.com/channel/UCzdTkBuxvZhOGQAN6eUsfWA", "user": "ipikalive"}, {"Comment": "I love how the taser desk literally constantly has sparks coming off of it which looks amazing, this is all thanks to that one comment", "Likes": "34", "Time": "3 months ago (edited)", "UserLink": "https://www.youtube.com/channel/UC_P1bdtzQhDgAnEZn1TyoBw", "user": "So confused"}, {"Comment": "Build something that looks absolutely terrifying but does nothing and just makes your friends paranoid", "Likes": "29K", "Time": "2 years ago", "UserLink": "https://www.youtube.com/channel/UCqMzTW0oC7B7HM6v8iiWIpg", "user": "Jerry Kat"}, {"Comment": "I love how everyone Michael knows is immediately scared when he talks to them", "Likes": "403", "Time": "1 year ago", "UserLink": "https://www.youtube.com/channel/UCqHG4WQJ7S3tUUo895WFBtQ", "user": "Evan Thorne"}]"#
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