import Foundation
let pattern = ##"(?:[A-Za-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
David Hills
Primeresllc@gmail.com
· Reply · 42w
Adrian Chu
badge icon
adrian@adrianchu.net
· Reply · 42w
Nick Bagoshvili
badge icon
Nick@ngbroofing.com
· Reply · 42w
Grace Wang
grace@ClarityPropertiesLLC.com
· Reply · 42w
Pete Venturo
That's my neighbor.
· Reply · 42w
Hom Teang
mrteang@gmail.com
· Reply · 42w
Derek Stephens
derek@summitreiholdings.com
· Reply · 42w
Julie K Clark
Julie@seattleinvestorsclub.com
· Reply · 42w
Alex Frankov
alexfrankov@hotmail.com
· Reply · 42w
Dawn Armstrong
Dawnarmstrong&hotmail.com
Outlook – free personal email and calendar from Microsoft
OUTLOOK.LIVE.COM
Outlook – free personal email and calendar from Microsoft
Outlook – free personal email and calendar from Microsoft
· Reply · 42w
Andrey Gulyy
Andreygulyyre@gmail.com please
· Reply · 42w
Viktor Bondarenko
viktor76@outlook.com
· Reply · 42w
Jeremy Macconnell
Sendmeyourdeals@foursqre.com
· Reply · 42w
A Suraphong Liengboonlertchai
Abuysellproperty@gmail.com. Thx 🙏
· Reply · 42w
Kellen Giroux
info@upraisedproperties.com
· Reply · 42w
Anthony Markiie Prado
Amprado@kw.com
· Reply · 42w
Jamil Newman
Westshoreinvestmentgroup@gmail.com
· Reply · 42w
Joe Clark
Josephclark10@gmail.com
· Reply · 42w
Nick Taitano
It's a great opportunity!
· Reply · 41w
Miao Jiang
mj@everestinvestments.net
· Reply · 41w
AJ Sheffield
aj@theoffmarketdeals.com
· Reply · 41w
Cindy Highsmith
Flyfreelw@gmail.com
· Reply · 41w
Steven Wang
stevenwproperties@gmail.com
· Reply · 41w
Miao Jiang
mj@everestinvestments.net
· Reply · 40w
Les Dreeson
ldreeson@listings.com
"""#
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