import Foundation
let pattern = #"\.populate_tasks\[(arc|buck|build|hg|phabricator|source_control),"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[arc,diff]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[arc,fix]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[arc,focus]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[arc,lint]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[arc,pull]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[arc,refocus]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[buck,build]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[buck,install]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[buck,project]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[buck,run]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[buck,test]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,arc_rebuild]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,buck_build]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,entschema]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,fbios_noextensions]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,instagram_ios_noextensions]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,messenger_ios_noextensions]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,multifeed]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,unicorn]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,wakizashi]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[build,wakizashi_s2p]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[devserver,arc_fix]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[devserver,fixmyserver]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[devserver,flow_oom]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[devserver,hack_oom]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[devserver,hhserver_oom]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hack,hh_healthy_minutes]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,amend]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,bookmarks]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,branch]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,commit]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,diff]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,log]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,pull]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,rebase]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,smartlog]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,status]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[hg,update]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[ide,flow_check]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[ide,flow_init]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[ide,hh_full_check]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[ide,hh_lazy_check]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[ide,hh_server_init]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[ide,intellij_file_to_intellisense]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[ide,intellij_project_to_intellisense]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[land,clean_lands]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[land,landcastle]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,diffusion_pageload]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,gql_client_duration]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,gql_mutation_duration]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,jf_submit_set_dependencies]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,jf_submit_set_diffproperty]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,jf_submit_total_elapsed]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,jf_submit_update_diff]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[phabricator,jf_submit_update_revision]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[presto,adhoc_queue]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[source_control,hg]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[source_control,jellyfish]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[test,local_www]
── infrastructure.devspeed.error_rates.devspeed_raw_cost_events.populate_tasks[test,mobilelab]
"""#
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