import Foundation
let pattern = #"^[\w\s./-]*(?:\s\d+){4}(?:\s\$\d+\.\d+){2}$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
Amount
2144-PL
1XL 2XL 3XL
2144-
PL Navy Blue 2 2 2 6 $11.50 $69.00
ETK-2097K-PL PLUS-TOP - BACK BUTTON TUNICS 95% RAYON 5% SPANDEX MADE IN USA
1XL 2XL 3XL
Black 2 2 2 6 $12.00 $72.00
Teal 2 2 2 6 $12.00 $72.00
ETK-2197-SW-
1XL 2XL 3XL
PL ETK-
2197- H.Grey/Burgu… 2 2 2 6 $14.00 $84.00
Off-White/Black 1 1 1 3 $14.00 $42.00
ETK-2143 Tops - 95% RAYON 5% SPANDEX MADE IN USA
S M L
Heather Grey 2 2 2 6 $10.50 $63.00
Royal Blue 2 2 2 6 $10.50 $63.00
Ruby Red 2 2 2 6 $10.50 $63.00
ETK2186-GD- Tops-Stripe Solid-95% Rayon 5% Spandex Made in USA
PL
1XL 2XL 3XL
Burgundy/Bur… 2 2 2 6 $11.00 $66.00
Ivory/Black 2 2 2 6 $11.00 $66.00
2139 - WP-PL PLUS TOP -95% RAYON 5% SPANDEX MADE IN USA
1XL 2XL 3XL
As Shown 2 2 2 6 $9.50 $57.00
ETK-2228
S M L
ETK-
2228 Off-White/Black 2 2 2 6 $9.50 $57.00
ETK-2149-PL
1XL 2XL 3XL
ETK-
2149- Taupe 2 2 2 6 $11.50 $69.00
BACK
ORDERED
White 2 2 2 6 $11.50 $69.00
Sub
"""#
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