import Foundation
let pattern = #"(?<!\d|-|\/|to|\$)(?<!\$(\d)\.)([1-9][0-9]{0,2}(\.\d{0,2})?\s?)(lbs|lb|LBS|LB|KG|kg|G|g|L|l+)(?!\d|\w)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
10lb Potato bag
Apples Gld Delicious 3lb
Grandfer Rocha Pear2.5lbs
CHIN.MAND.5LB
Batata Veg (10LB)
SIG 170LB99 Legging SzB 1ea
ARJO CDA1450035 SupBlk 400LB1ea
AHC EB205L
DRI 10220-1 Bariatric500lb 1 ea
TILDA SONA MASOORI 20 LBS
OVAL ROASTER 9 - 12 LBS
ORGANIC TRIPACK FRUIT 1.2 KG
KGF5KT2404 PEPLUM T, PURPLE
AIR 1773LB-XL 15-20 KnXL 1ea
PLUM BAG 1KG US
NEXT XTRA 100G
SUDOCREM 60G
EGGPLANT GRILLED IN OIL (3KG)
CORONATION GRAPE 2L CA
MEAT DEAL $11.00 KG
LIVE LOBSTER- 2.00-3.00 LB-MSC
PC MAGIC GROW 20-20-20 3.55KG
KGF7W3409 KGVELCROWB,GREY
TZATZIKI 500G
2+1 LEAD MIX
"""#
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