Skip to content

Instantly share code, notes, and snippets.

@fitomad
Last active November 14, 2017 10:05
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 fitomad/f2e3d067e5470994f8edb10e593c620e to your computer and use it in GitHub Desktop.
Save fitomad/f2e3d067e5470994f8edb10e593c620e to your computer and use it in GitHub Desktop.
import Foundation
let tweet: String = "Detectar etiquetas y usuario en un tuit con #Swift. También podemos recoger los usuario como @fitomad o @AppStore. #SwiftLang #iOSDev #macOSDev"
// Estos caracteres no nos interesan...
var skip_set: CharacterSet = CharacterSet.whitespacesAndNewlines
skip_set.formUnion(CharacterSet.punctuationCharacters)
// ...excepto estos, que son para usuarios y etiquetas y no debemos excluirlos.
skip_set.remove(charactersIn: "@#_")
// Los caracteres de lo que buscamos (usuarios y etiquetas)
let twitter_set: CharacterSet = CharacterSet(charactersIn: "@#")
let scanner: Scanner = Scanner(string: tweet)
scanner.charactersToBeSkipped = skip_set
repeat
{
_ = scanner.scanUpToCharactersFromSet(twitter_set)
if let twitter_token = scanner.scanUpToCharactersFromSet(skip_set)
{
print(twitter_token)
}
} while !scanner.isAtEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment