Skip to content

Instantly share code, notes, and snippets.

@fitomad fitomad/tweet-scan.swift
Last active Nov 14, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.