import Foundation
let pattern = #".*@softcomputer\.com>? *(?:"? *<\S+> *)?$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
== Real scc
Mary Bills <marybil@softcomputer.com>
<ellie@softcomputer.com>
<thereasaw@softcomputer.com>
test@softcomputer.com
<test@softcomputer.com>
olexiyry@softcomputer.com <olexiyry@softcomputer.com>
Deseriee Harkins <deseriee@softcomputer.com>
== Forged scc
Dorota Mazurek <dorotam@softcomputer.com> <reservations@nesthotel.com>
Nikki Taft <nicolet@softcomputer.com> <Muhammad.owais@abtach.com>
Lynette Didia <ldidia@softcomputer.com> <Muhammad.owais@abtach.com>
Dorota Mazurek <dorotam@softcomputer.com> <centralbookings@kbcsa.co.za>
Jeffrey Marr <jeffreym@softcomputer.com> <srvadv5.blr@vw-ppsmotors.co.in>
Iryna Dmytriyeva @SCC <idmy@softcomputer.com> <sc@grupoinrexsa.com>
Vickie Nix vickieh@softcomputer.com <m.recovery@albarondebt.com>
Bill Young <billyo@softcomputer.com> <sara@eaurenaissance.com>
== Questionable
"Kurt Veith <kurtv@softcomputer.com>" <marlene.robles@mld.com.mx>
"softcomputer.com" <MAIL.QUOTA@servers.com>
"marksm@softcomputer.com" <mark560sl@gmail.com>
== Valid non-forged addresses
"sarahze@softcomputer.com via Smartsheet" <user@smartsheet.com>
"yuvi softcomputer.com" <hugginshospital.notification@zixmessagecenter.com>
<technicalsupport@softcomputer.com.au>
"Secure Email From yevgeny@softcomputer.com via Message Pickup Center" <emx-intermedia@securemail.intermedia.net>
"Secure Email From yevgeny@softcomputer.com via Message Pickup Center" <emx-intermedia@securemail.intermedia.net>
"mpettis@softcomputer.com via SurveyMonkey" <member@surveymonkeyuser.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