0
func getEmojiTags(text: String) -> [String] {
}

An emoji tag is a combination of two parts, with no spaces between them. If there is a space between them, then it is not an emojiTag.

  • a character which is an emoji
  • a string which is not an emoji

For example:

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]
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 2
    If Swift handles unicode properly, they should be treated as a single character, so you should just be able to check if they're in one of a few character ranges. – Blender Aug 11 '16 at 22:59
  • 1
    @Blender so, what are the ranges? I think, the question is about this – Mehdi Khademloo Aug 11 '16 at 23:20
  • @MehdiKhademloo: That's why I posted a comment, not an answer. They shouldn't be hard to find. – Blender Aug 11 '16 at 23:38

1 Answers1

1

Try using NSRegularExpression with emoji code ranges.

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"]
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • Does this detect all emoji? How can I detect every emoji? – TIMEX Aug 14 '16 at 00:55
  • @TIMEX, "emoji" does not have a clear and common definition. You need to define what characters you want to treat as emoji in your app. – OOPer Aug 14 '16 at 01:03
  • @TIMEX, [This UTR #51](http://unicode.org/reports/tr51/) is trying to provide _data that designates which characters are considered to be emoji_ . – OOPer Aug 14 '16 at 01:54