import Foundation
// WARNING: You included a flag that Swift doesn't support: U
// When this flag is set, it inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by '?'.
// As an alternative, this effect can also be achieved by setting a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).
let pattern = #""name":"([^"]+)""#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"{"code":200,"message":"OK","data":[{"_id":"56b76689d669e92204089e7f","added":1454859913237,"songid":"56430e7446642c2d00adca7c","_song":{"_id":"56430e7446642c2d00adca7c","name":"Phyreum - Levitate","description":"Chill.\n\nPhyreum\nhttps://soundcloud.com/phyreum\n\nFollow the Panda on Soundcloud\nhttps://soundcloud.com/chillingpandas","images":{"thumbnail":"https://i.ytimg.com/vi/4bLR44pzO08/hqdefault.jpg","youtube":{"default":{"url":"https://i.ytimg.com/vi/4bLR44pzO08/default.jpg","width":120,"height":90},"medium":{"url":"https://i.ytimg.com/vi/4bLR44pzO08/mqdefault.jpg","width":320,"height":180},"high":{"url":"https://i.ytimg.com/vi/4bLR44pzO08/hqdefault.jpg","width":480,"height":360},"standard":{"url":"https://i.ytimg.com/vi/4bLR44pzO08/sddefault.jpg","width":640,"height":480},"maxres":{"url":"https://i.ytimg.com/vi/4bLR44pzO08/maxresdefault.jpg","width":1280,"height":720}}},"type":"youtube","fkid":"4bLR44pzO08","songLength":218000,"created":1447235188926,"updub":0,"downdub":0,"userid":null,"__v":0},"playlistid":"5643a672021b872200788d83","__v":0,"order":119},{"_id":"56b7670c54e33169028bbd6e","added":1454860044728,"songid":"56b7567636ffb39b03574f02","_song":{"_id":"56b7567636ffb39b03574f02","name":"Desired Bit- The White Moon","images":{"thumbnail":"https://i1.sndcdn.com/artworks-000146208197-a3zi7s-large.jpg","soundcloud":{"artwork_url":"https://i1.sndcdn.com/artworks-000146208197-a3zi7s-large.jpg","waveform_url":"https://w1.sndcdn.com/wl57ni165Vi7_m.png"}},"genre":"Chill","type":"soundcloud","fkid":"245853471","streamUrl":"https://api.soundcloud.com/tracks/245853471/stream","songArtist":"71616267","songLength":301507,"created":1454855798080,"updub":0,"downdub":0,"userid":null,"__v":0},"playlistid":"5643a672021b872200788d83","__v":0,"order":120},{"_id":"56b76a28472a9d610424806f","added":1454860840335,"songid":"56b2074556459d1d00a7529d","_song":{"_id":"56b2074556459d1d00a7529d","name":"Alice On The Roof - Mystery Light (Curtis Alto Remix)","description":"Thank u guys for 120k its been an insane journey for us to share so mush music to more and more people here is our gift to you!\nIf you like the Track, please press ♥ & ♻ to share it with your friends <3\n\nFOLLOW:@TheFallingApple\nFacebook:www","images":{"thumbnail":"https://i1.sndcdn.com/artworks-000144025225-zbqcix-large.jpg","soundcloud":{"artwork_url":"https://i1.sndcdn.com/artworks-000144025225-zbqcix-large.jpg","waveform_url":"https://w1.sndcdn.com/PzlmEyFwaK0d_m.png"}},"genre":"Deep House","type":"soundcloud","fkid":"243086787","streamUrl":"https://api.soundcloud.com/tracks/243086787/stream","songArtist":"171627625","songLength":203851,"created":1454507845017,"updub":0,"downdub":0,"userid":null,"__v":0},"playlistid":"5643a672021b872200788d83","__v":0,"order":121},{"_id":"56b7727b7469f0a403055757","added":1454862971589,"songid":"560a1d0adf36550300a5aab6","_song":{"_id":"560a1d0adf36550300a5aab6","name":"Bonobo - Kong","description":"Produced with CyberLink PowerDirector 9\r\n\r\nBonobo, the stage name of Simon Green (born 30 March 1976), is a British musician, producer and DJ.","images":{"thumbnail":"https://i.ytimg.com/vi/tgcr1rE-lPw/hqdefault.jpg","youtube":{"default":{"url":"https://i.ytimg.com/vi/tgcr1rE-lPw/default.jpg","width":120,"height":90},"medium":{"url":"https://i.ytimg.com/vi/tgcr1rE-lPw/mqdefault.jpg","width":320,"height":180},"high":{"url":"https://i.ytimg.com/vi/tgcr1rE-lPw/hqdefault.jpg","width":480,"height":360},"standard":{"url":"https://i.ytimg.com/vi/tgcr1rE-lPw/sddefault.jpg","width":640,"height":480},"maxres":{"url":"https://i.ytimg.com/vi/tgcr1rE-lPw/maxresdefault.jpg","width":1280,"height":720}}},"type":"youtube","fkid":"tgcr1rE-lPw","streamUrl":null,"fileUrl":null,"songLength":239000,"songBitrate":null,"songMeta":null,"created":1443503370553,"updub":0,"downdub":0,"userid":null,"__v":0},"playlistid":"5643a672021b872200788d83","__v":0,"order":122},{"_id":"56b78abc3e6037bf0312d1fd","added":1454869180105,"songid":"560309f6e86321030046710c","_song":{"_id":"560309f6e86321030046710c","name":"Tom Day - Peaks","description":"Enjoy!\nDownload...http://bit.ly/1Ie9GeG\nSupport on iTunes...http://apple.co/1JLU1WM\n\nTom Day\nhttps://soundcloud.com/tomday\nhttps://www.facebook.com/tomdaymusicaus\nhttps://twitter.com/tomdaymusic\n\nArtwork by Bang\nhttp://www.pixiv.net/member_illust.php?mode=medium&illust_id=43287611\nhttp://www.pixiv.net/member.php?id=10746425\n\nFacebooobs\nhttps://www.facebook.com/MrSuicideSheep\n\nSheepy tees\nhttp://bit.ly/Sheepytees\n\n\n*If any producer or label has an issue with any of the uploads please get in contact (suicide_sheep@hotmail.com) with me and I will delete it immediately (this includes artists of the images used).","images":{"thumbnail":"https://i.ytimg.com/vi/JbEbZGb6fhU/hqdefault.jpg","youtube":{"default":{"url":"https://i.ytimg.com/vi/JbEbZGb6fhU/default.jpg","width":120,"height":90},"medium":{"url":"https://i.ytimg.com/vi/JbEbZGb6fhU/mqdefault.jpg","width":320,"height":180},"high":{"url":"https://i.ytimg.com/vi/JbEbZGb6fhU/hqdefault.jpg","width":480,"height":360},"standard":{"url":"https://i.ytimg.com/vi/JbEbZGb6fhU/sddefault.jpg","width":640,"height":480},"maxres":{"url":"https://i.ytimg.com/vi/JbEbZGb6fhU/maxresdefault.jpg","width":1280,"height":720}}},"type":"youtube","fkid":"JbEbZGb6fhU","streamUrl":null,"fileUrl":null,"songLength":454000,"songBitrate":null,"songMeta":null,"created":1443039734247,"updub":0,"downdub":0,"userid":null,"__v":0},"playlistid":"5643a672021b872200788d83","__v":0,"order":123},{"_id":"56b8a52094a7e4eb011171ed","added":1454941472432,"songid":"561083368dc10c030067c5a4","_song":{"_id":"561083368dc10c030067c5a4","name":"Carbon Based Lifeforms - Gryning","description":"Carbon Based Lifeforms - Gryning From Album World Of Sleepers\nhttp://ultimae.bandcamp.com/album/world-of-sleepers","images":{"thumbnail":"https://i.ytimg.com/vi/P9Xd3z29Lys/hqdefault.jpg","youtube":{"default":{"url":"https://i.ytimg.com/vi/P9Xd3z29Lys/default.jpg","width":120,"height":90},"medium":{"url":"https://i.ytimg.com/vi/P9Xd3z29Lys/mqdefault.jpg","width":320,"height":180},"high":{"url":"https://i.ytimg.com/vi/P9Xd3z29Lys/hqdefault.jpg","width":480,"height":360},"standard":{"url":"https://i.ytimg.com/vi/P9Xd3z29Lys/sddefault.jpg","width":640,"height":480},"maxres":{"url":"https://i.ytimg.com/vi/P9Xd3z29Lys/maxresdefault.jpg","width":1280,"height":720}}},"type":"youtube","fkid":"P9Xd3z29Lys","songLength":384000,"created":1443922742501,"updub":0,"downdub":0,"userid":null,"__v":0},"playlistid":"5643a672021b872200788d83","__v":0,"order":124},{"_id":"56b93a1daa3022f104d2af70","added":1454979613568,"songid":"5658e0783b6ea81300b33153","_song":{"_id":"5658e0783b6ea81300b33153","name":"Purity RIng - Obedear","description":"From the album - Shrines\niTunes: https://itunes.apple.com/us/album/shr...\n\nhttp://purityringsongs.com\nhttp://www.facebook.com/purityringmusic\nhttp://lastgangentertainment.com/records","images":{"thumbnail":"https://i.ytimg.com/vi/VZ7Wsfg-dbU/hqdefault.jpg","youtube":{"default":{"url":"https://i.ytimg.com/vi/VZ7Wsfg-dbU/default.jpg","width":120,"height":90},"medium":{"url":"https://i.ytimg.com/vi/VZ7Wsfg-dbU/mqdefault.jpg","width":320,"height":180},"high":{"url":"https://i.ytimg.com/vi/VZ7Wsfg-dbU/hqdefault.jpg","width":480,"height":360},"standard":{"url":"https://i.ytimg.com/vi/VZ7Wsfg-dbU/sddefault.jpg","width":640,"height":480},"maxres":{"url":"https://i.ytimg.com/vi/VZ7Wsfg-dbU/maxresdefault.jpg","width":1280,"height":720}}},"type":"youtube","fkid":"VZ7Wsfg-dbU","songLength":210000,"created":1448665208710,"updub":0,"downdub":0,"userid":null,"__v":0},"playlistid":"5643a672021b872200788d83","__v":0,"order":125}]}"#
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