import Foundation
let pattern = #"\w.*solarwinds.orion\\(solarwinds.*|swjobengine.*|netflowservice.*|counterfetcher.*)\.exe$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
// Explanation
// Matches all files part of the "orion" folder that start with solarwinds,swjobengine,netflowservice,counterfetcher and ends with ".exe"
// Some Solarwinds exe starting with "solarwinds."
e:\program files\solarwinds\orion\solarwinds.orion.logmgmt.syslogservice.exe
e:\program files\solarwinds\orion\solarwinds.businesslayerhostx64.exe
e:\program files\solarwinds\orion\solarwinds.orion.logmgmt.syslogservice.exe
e:\program files\solarwinds\orion\solarwinds.collector.service.exe
e:\program files\solarwinds\orion\solarwinds.orion.logmgmt.pollingservice.exe
e:\program files\solarwinds\orion\solarwinds.businesslayerhost.exe
e:\program files\solarwinds\orion\solarwinds.orion.logmgmt.trapservice.exe
e:\program files\solarwinds\orion\solarwinds.recommendations.service.exe
e:\program files\solarwinds\orion\solarwinds.informationservice.servicev3.exe
e:\program files\solarwinds\orion\solarwinds.highavailability.service.exe
e:\program files\solarwinds\orion\solarwinds.servicehost.process.exe
e:\program files\solarwinds\orion\solarwinds.topology.calculator.exe
// Example with another drive
C:\program files\solarwinds\orion\solarwinds.topology.calculator.exe
// Some Solarwinds exe NOT starting with "solarwinds."
e:\program files\solarwinds\orion\netflowservice.exe
e:\program files\solarwinds\orion\swjobengineworker2.exe
e:\program files\solarwinds\orion\swjobenginesvc2.exe
e:\program files\solarwinds\orion\swjobengineworker2x64.exe
e:\program files\solarwinds\orion\counterfetcher.exe
// Non Orion folders
c:\program files\common files\solarwinds\administrationservice\solarwinds.administration.exe
// TESTS
e:\program files\solarwinds\orion\solarwinds.exe
e:\program files\solarwinds\orion\solarwinds.test.exe
e:\program files\solarwinds\orion\solarwinds.test.test2.exe
Without orion folder
e:\program files\solarwinds\NotOrion\solarwinds.servicehost.process.exe
"""#
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