import Foundation
let pattern = #"""
^(?P<operating_pressure>H)(?P<sep_1>-)(?P<number_of_main_ports>4)(?P<unit_type>W)(?P<actuation_type>EH|H)(?P<unit_size>62)(?P<spool_feedback_main_stage>H|)(?P<spool_symbol>C|D|K|Z|E|F|G|H|J|L|M|Q|R|S|T|U|V|W)(?P<unit_series>5X)(?P<mounting_type>F|)(?P<sep_2>/)(?P<spool_feedback>OF|O)(?P<pilot_valve>10)(?P<type_of_solenoid>A|L)(?P<solenoid_in_ac_or_dc_version>G|W)(?P<voltage_of_solenoid>220|195|180|127|120|110|96|60|42|24|12)(?P<voltage_rectifier>R|-|)(?P<manual_override_unit>Y|)(?P<spool_position_monitoring>Y|)(?P<unit_for_time_adjustment>S2|S|)(?P<electrical_connection>Z5L|Z2L|Z1L|DKL|DZL|DL|DZ|DK|ZL|KL|Z5|Z4|Z2|Z1|D|L|Z|K|)(?P<sep_3>/)(?P<additional_equipment>17|16|15|14|13|12|11|10)(?P<orifice_positon>B|)(?P<orifice_diameter>15|12|11|10|08|)(?P<seal_material>V|)(?P<additional_details>)$
"""#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
H-4WEH62HE5XF/10EG24N9S2K4
H-4WEH62HD5XF/OF10EG220N9SK4/B10V
H-4WEH62HH5XF/10EG24N9K4
"""#
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