Skip to content

Instantly share code, notes, and snippets.

@abargh
Last active March 2, 2018 14:05
Show Gist options
  • Save abargh/b3193814043c004b565a070faf560158 to your computer and use it in GitHub Desktop.
Save abargh/b3193814043c004b565a070faf560158 to your computer and use it in GitHub Desktop.
Extension to String to Extract Hash Tags (source: https://learnappmaking.com/regular-expressions-swift-string/)
extension String {
func hashtags() -> [String] {
guard let regex = try? NSRegularExpression(pattern: "#[a-z0-9]+", options: .caseInsensitive) else {
return []
}
let string = self as NSString
return regex.matches(in: self, options: [], range: NSRange(location: 0, length: string.length)).map {
string.substring(with: $0.range).replacingOccurrences(of: "#", with: "").lowercased()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment