import Foundation
let pattern = #"(((([0-9])?[A-Z]{1,})(([A-Z0-9./+]+){0,}-){1,})[A-Z0-9./+]+\=*)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
The ABC-CD40N= is also supported on GH-K slots on the 4000 Series ISRs using the XYZ-X-THM . This SM-X-NIM-REW34= information is not captured in the table above terms with only digits shoud not match --> 1-800-553-6387 hdhdhh IP-IP
YUIO-10GB-BG4: Supports JK-X6824-UIO-XK++= U-VI1.1-100-WX-Y9
VA-V
XX-123-UVW-3456
VA-V-W-K9
VA-V-W
800-CVB-4=
CD-EF-GH-40G-R9(=)
"""#
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