func getEmojiTags(text: String) -> [String] {
}
表情符号标签是两部分的组合,它们之间没有空格。如果它们之间有空格,那么它不是表情符号。
例如:
Hello, my name is Jason how are you ?-> []
Hello, my name is Jason -> [Jason]
Hello, my name is Jason -> []
I am going to the ⛱beach with some monkeys -> [⛱beach, monkeys]
答案 0 :(得分:1)
尝试将NSRegularExpression
与表情符号代码范围一起使用。
func emojiTags(str: String) -> [String] {
//A little bit simplified, you may need to define what are your "emoji".
//This is a subset defined in http://stackoverflow.com/a/36258684/6541007 .
let emojiCharacters = "\\U0001F600-\\U0001F64F\\U0001F300-\\U0001F5FF\\U0001F680-\\U0001F6FF\\u2600-\\u26FF"
let pattern = "[\(emojiCharacters)][^\(emojiCharacters)\\s]+"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matchesInString(str, options: [], range: NSRange(0..<str.utf16.count))
return matches.map{(str as NSString).substringWithRange($0.range)}
}
let str1 = "Hello, my name is Jason how are you ?"
print(emojiTags(str1)) //->[]
let str2 = "Hello, my name is Jason"
print(emojiTags(str2)) //->["Jason"]
let str3 = "Hello, my name is Jason"
print(emojiTags(str3)) //->[]
let str4 = "I am going to the ⛱beach with some monkeys "
print(emojiTags(str4)) //->["⛱beach", "monkeys"]