import Foundation
let pattern = #"(?i)\b(Devices|Community String)\b\s*-\s*([\w,\/]+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
1. What is being changed?
Add in Solarwinds monitoring the router and switch of the new Store:
Devices - MCHLS5178,sw517801,sr517800,sr517801,sr517802,MCHLS5184,sw518401,sr518400,sr518401,sr518402
Community String - N/A
2. Where it is being changed?
Solarwinds monitoring (10.3.4.7)
3. Why it is being changed?
To monitor the router and switch of the store 5178,5184
4. What is the Business Impact of the Change?
No business impact during implementation.
5. Who will be doing the change?
Network team
6. When is the change happening?
2020-01-26 01:00:00
7. Who will be testing the change after implementation?
Network team
"""#
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