Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created October 28, 2017 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanislavK/4a651e79057c9d4dc23b495afcea8078 to your computer and use it in GitHub Desktop.
Save StanislavK/4a651e79057c9d4dc23b495afcea8078 to your computer and use it in GitHub Desktop.
extension String {
/** Get email addresses in a string, discard any other content. */
func emailAddresses() -> [String] {
var addresses = [String]()
if let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) {
let matches = detector.matches(in: self, options: [], range: NSMakeRange(0, self.count))
for match in matches {
if let matchURL = match.url,
let matchURLComponents = URLComponents(url: matchURL, resolvingAgainstBaseURL: false),
matchURLComponents.scheme == "mailto"
{
let address = matchURLComponents.path
addresses.append(String(address))
}
}
}
return addresses
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment