import Foundation
let pattern = #"Title: (?<Title>.*$)[\s\S]*(?<PhoneNumber>\d{10})[\s\S]*When: (?<Date>.[a-zA-Z]*\s.\d*\s.[a-zA-Z]*\s\d*)\s(?<Time>.[0-9]*:[0-9]*[am|pm]* \S [0-9]*:[0-9]*[am|pm]*)[\s\S]*Where: (?<Where>.*?.*$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
You have been invited to the following event.
Title: לקוח רועי
0544710385
When: Sat 28 Nov 2020 9:30pm – 10:30pm Israel Time
Where: Mazal Keshet Street, Mazal Keshet St, Hod Hasharon, Israel
Joining info: Join with Google Meet
https://meet.google.com/ngv-fxvo-oyt
Calendar: mplmp5e1vpvapd7fl5euj7g81ytfzlem@hook.integromat.com
Who:
* ecmanager8@gmail.com- organiser
* My Automation
Event details:
https://calendar.google.com/calendar/event?action=VIEW&eid=NnNvbTJjMW82Z3I2YWJiMmNvbzM2YjlrYzloNmFiOW82Y3FtYWJiMWM0cDNnb3I1NzBzMzJkOXA2YyBtcGxtcDVlMXZwdmFwZDdmbDVldWo3ZzgxeXRmemxlbUBob29rLmludGVncm9tYXQuY29t&tok=MjAjZWNtYW5hZ2VyOEBnbWFpbC5jb20yODdjNWU1ZjU2ZjVjNWIwZmE4NmYwOTMxNzY5NTczMGNhNGJiN2E3&ctz=Asia%2FJerusalem&hl=en_GB&es=0
Invitation from Google Calendar: https://calendar.google.com/calendar/
You are receiving this courtesy email at the account
mplmp5e1vpvapd7fl5euj7g81ytfzlem@hook.integromat.com because you are an
attendee of this event.
To stop receiving future updates for this event, decline this event.
Alternatively, you can sign up for a Google Account at
https://calendar.google.com/calendar/ and control your notification
settings for your entire calendar.
Forwarding this invitation could allow any recipient to send a response to
the organiser and be added to the guest list, invite others regardless of
their own invitation status or to modify your RSVP. Learn more at
https://support.google.com/calendar/answer/37135#forwarding
"""##
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