import Foundation
let pattern = #"(?P<User>[a-zA-Z1-9\._]+)@(?P<Domain>[a-zA-Z1-9]+)\.(?P<D_EXT>[a-zA-Z]+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
my email is andy@delusion.org
my school email is andrew.newton18@mydiscoveryk8.org
daddy's email is mike@delusion.org
Zach H's email is hitchcockzach@gmail.com
Zach R's email is zrambler441@yahoo.com
Ryan's email is ryanmeth@icloud.com
Mateo's email is mateo@relhok.com
Kieran's email is kieran.derrington@gmail.com
Tyler's email is tbone.vaughn@icloud.com
DON'T WORK:
...@.......
secreretface-2@loans.com
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"${User} at ${Domain}.${D_EXT}\n"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
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