import Foundation
let pattern = #"(?i)Store number – ([\d\D]*)\n.*StreetA – ([\d\D]*)\n.*City – ([\d\D]*)\n.*State_id – ([\d\D]*)\n.*Postal Code – ([\d\D]*)\n.*Store Name – ([\d\D]*)\n.*Phone Number – ([\d\D]*)\n.*Tax Rates.*\n.*Tax table 1 – ([\d\D]*)\n.*Tax table 2 – ([\d\D]*)\n.*Tax table 3 – ([\d\D]*)\n.*Tax table 4 – ([\d\D]*)\n.*Tax table 5 – ([\d\D]*)\n.*Artistree vendor – ([\d\D]*)\n.*Store Group – ([\d\D]*)\n.*Store Zone number – ([\d\D]*)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
FP Team,
Please install the Live database for store 1384. The updates have been completed.
• Store number – 1384
• StreetA – 1275 York Rd, Ste 21A
• City – Gettysburg
• State_id – PA
• Postal Code – 17325-7565
• Store Name – Gettysburg, PA
• Phone Number – (717) 420-8200
• Tax Rates
o Tax table 1 – 6.0000%
o Tax table 2 – 0.0000%
o Tax table 3 – 0.0000%
o Tax table 4 – 0.0000%
o Tax table 5 – N/A
• Artistree vendor – Kernersville
• Store Group – US.201308
• Store Zone number – 2030
"""#
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