import Foundation
let pattern = #"(\d{5,}\S+)(?:-REV)|(\d{5,})|(\d.+)(?:-REV).+|(\b[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+\b)|(\d{4,}.\S+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
SIEMENS10249071REV05
22D1496-REV-A
SANTASISE398FOP4
KCC C318-M-2328
22006-17114-REV-0
WELD30
2328ENDFIN
OSH4780973
10520398ITEM7
KCC C153-M-136290
2373 TAPER
SANTA ISE398 R2
SANTASISE398RGH1
34F0-010-102-REV-WELDMENT
14A1936D1F1-REV-NONE
6181194-REV-B
PRETE WT-80-1015BLIND
MAXAM340000566
SHARPE-36465 1ST
OSH 3899580
7494-03-301-REV-NONE
MAXAM340000566OP2
KCC C318-M-2373TAPER
CARDINAL36465
SIEMENS-10248227 REV.8
4212.8842-REV-Q
14A1936-REV-NONE
LATHE TOOL
360 YIELD 450111 RH MAIN PROGRAM;360 YIELD 450111 LH MAIN PROGRAM;450111
KCC C318-M-2373
360 YIELD 450211 RH MAIN PROGRAM;360 YIELD 450211 LH MAIN PROGRAM;450211
360 YIELD 450101 LH MAIN PROGRAM;450101
10753289-REV-02
30613D3-REV-05
30613D4-REV-05
OSHKOSH CORP/4381568/REV B/OP 1
OSHKOSH/12611249/REV A/OP1
14A1669 COVER-REV-J
OSH 4641290
KCC C318-M-2372
2328 FINISH
10751755-REV-2
312-133001-001-REV-D
KOOTR KT300AL OP1
OSHKOSH CORP/4381568/REV B/OP 2
360 YIELD 450201 LH MAIN PROGRAM;360 YIELD 450201 RH MAIN PROGRAM;450201
OSH 3899580OP2
10520398ITEM17
WELD30BIG
2347337-01-REV-NONE
2372 FINISH
10753199-02-REV-7
OSH 1693510
2328END
OSH 4384499OP2
E4-TKD-Z002A00KP;E4-TKD-ZOO2AOO WITH TRANSFER
10753199-02-REV-6
OSH 4384499
7494-04-103-REV-NONE
14A1669D2F1-REV-J
4212.8801-REV-O
2373 FINISH
2372 TAPER
SHARPE-36465 2ND
KOOTR KT300AL OP2
312-133034-001-REV-B
14A1937-REV-01
OSHKOSH/12611249/REV A/OP2
SANTASISE398FOP3
SIEMENS10248226REV08
"""#
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