import Foundation
let pattern = #"^.*?([0-9]+-[a-zA-Z]{2,}-[0-9]+).*"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
"NE-390-SM-04"
--> "390-SM-04"
"90055-SL-01-J"
--> "90055-SL-01"
"NE-1478-SL-02"
--> "1478-SL-02"
"87007-QM-01-J"
--> "87007-QM-01"
"NE-9315-BM-01-A"
--> "9315-BM-01"
"3121-SP-01"
--> "3121-SP-01"
"1639-YL-01"
--> "1639-YL-01"
"NE-9922-WM-01-J"
--> "9922-WM-01"
"ND-2498-YL-01"
--> "2498-YL-01"
"C-4008-PP-03-J"
--> "4008-PP-03"
"876-C-4008-PP-03-J-234"
--> "4008-PP-03"
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"\1"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
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