import Foundation
let pattern = #"(?:CN=)(\w+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"CN=Lst-niketech.cis.splunk.admins,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=Lst-niketech.cis.splunk.users,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=Lst-NikeTech.CIS.SOC,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=ACL.Shareddata.CIS.Shared.Operations.WHQ.Change,OU=ACL,OU=Groups,DC=ad,DC=nike,DC=com|CN=Shareddata.CIS.Shared.WHQ.Change,OU=SharedData,OU=Groups,DC=ad,DC=nike,DC=com|CN=SharedMail.Information.Security.Incident.Reporting.WHQ,OU=SharedMail,OU=Groups,DC=ad,DC=nike,DC=com|CN=Lst-digitaltech.splunk.security.users,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=Lst-digitaltech.splunk.security.lead,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=Application.SolarWinds.Xerox.Nike.Users,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=Application.Citrix.Nike.Employees.Users,OU=Citrix,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=Nike.NikeTech.NIS.SecOps,OU=Nike,OU=Groups,DC=ad,DC=nike,DC=com|CN=Application.Splunk.CIS.Admin.Users,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=lst-Nike.Cloud.Change.Management,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=Application.TimeTrack.Users,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=SharedMail.nismonitor.WHQ,OU=SharedMail,OU=Groups,DC=ad,DC=nike,DC=com|CN=Application.SCCM.Reporting.AM,OU=SCCM,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=Application.Casper.Report.User,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=Application.HR.Portal.ESS.NonRetail,OU=Portal,DC=ad,DC=nike,DC=com|CN=Application.HR.Portal.ESS.US,OU=Portal,DC=ad,DC=nike,DC=com|CN=Application.Hightail.Users,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=Lst-FE.All,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=Lst-All.Exchange.Mailboxes BEAVERTN-SVR-VB,OU=Lists,OU=Groups,DC=ad,DC=nike,DC=com|CN=Application.Citrix.WebEx.Users,OU=Citrix,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=Lst-NikeTech.CIS.ITSecurity.All,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=Lst-Metaframe.Global.Users,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=SharedData.GIPS_AllStar.WHQ.Read,OU=SharedData,OU=Groups,DC=ad,DC=nike,DC=com|CN=Application.citrix.shared.webex.users,OU=Citrix,OU=Application,OU=Groups,DC=ad,DC=nike,DC=com|CN=Lst-NikeTech.CIS.SecOps,OU=Lists,OU=BEAVERTN,OU=OR,OU=USA,DC=ad,DC=nike,DC=com|CN=SharedMail.Firewall.Changes.WHQ,OU=SharedMail,OU=Groups,DC=ad,DC=nike,DC=com|CN=SharedMail.Nike.Information.Security.WHQ,OU=SharedMail,OU=Groups,DC=ad,DC=nike,DC=com|CN=SharedMail.IOS.SSLCert.Requests.WHQ,OU=SharedMail,OU=Groups,DC=ad,DC=nike,DC=com"#
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