import Foundation
let pattern = #"Departure Date\/Time:(.{2,500}?\s)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
Document/Message Date/Time:2006-11-15T18:07 Booking Date/Time: 2006-11-15T19:00
Arrival Date/Time:2006-11-15T21:00 Departure Date/Time: 2006-11-15T21:30
Free Text
Goods Description: 1 PALLETS RECEIVED DAMAGED 1234561 PALLETS RECEIVED DAMAGED 123456
Non-acceptance Information:1 PALLETS RECEIVED DAMAGED 1234561 PALLETS RECEIVED DAMAGED 123456
Details of Transport
Haulier Number:ABCD5678901234567
Haulier’s Name:CARRIERS NAME456789 Wkjhutuytyut
Name and Address Message
Tesco Receiving Centre Number: AP3456789 Wduiyiu
Depot Number:CLP456789 WzkjdhusWE
Name & Address Description:DEPOT NAME Wdkwadyuw786X
Location Identification
Place/Port of Loading:AA3456789012345678901234xPlace of Transhipment:CC3456789012345678901234x
Place/Port of Discharge: BB3456789012345678901234x Bording Crossing Place:DD3456789012345678901234x
"""#
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