2016-08-24 68 views
2

我必須解析一些字符串並提取emojis。從NSString中提取表情符(unicode)

我找不到任何好的解決方案。

讓我們說,我有這個字符串:

「XXX SSS」

我怎樣才能得到這2種表情符號?

需要注意的是:「== +(繪文字修改菲茨帕特里克的Type-6)」

+0

你可以嘗試一些正則表達式 –

+0

關於表情符號修飾符,比較http://stackoverflow.com/questions/39104152/how-to-know-if-two-emojis-will-be-displayed-as-one -emoji。 –

+5

這是你在找什麼http://stackoverflow.com/questions/35106059/how-to-extract-emojis-from-a-string? –

回答

1

此代碼只能在最新的iOS 10 SDK,並不能檢測某些組合表情符號,但如果這可以嗎,請嘗試:

func emojis(_ str: String) -> [String] { 
    //You may need to add some extra characters as "Umbrella on ground" does not have property "Emoji_Presentation". 
    let emojiPattern1 = "[\\p{Emoji_Presentation}\\u26F1]" //Code Points with default emoji representation 
    let emojiPattern2 = "\\p{Emoji}\\uFE0F" //Characters with emoji variation selector 
    let emojiPattern3 = "\\p{Emoji_Modifier_Base}\\p{Emoji_Modifier}" //Characters with emoji modifier 
    let emojiPattern4 = "[\\U0001F1E6-\\U0001F1FF][\\U0001F1E6-\\U0001F1FF]" //2-letter flags 
    let pattern = "\(emojiPattern4)|\(emojiPattern3)|\(emojiPattern2)|\(emojiPattern1)" 
    let regex = try! NSRegularExpression(pattern: pattern, options: []) 
    let matches = regex.matches(in: str, options: [], range: NSRange(0..<str.utf16.count)) 
    return matches.map{(str as NSString).substring(with: $0.range)} 
} 

print(emojis(" xxx sss ")) //->["", ""] 

(更新)刪除在上面的代碼中的一些錯誤。有一點嘗試檢測一些組合的emojis。

func emojisIncludingCombined(_ str: String) -> [String] { 
    //You may need to add some extra characters as "Umbrella on ground" does not have property "Emoji_Presentation". 
    let emojiPattern1 = "[\\p{Emoji_Presentation}\\u26F1]" //Code Points with default emoji representation 
    let emojiPattern2 = "\\p{Emoji}\\uFE0F" //Characters with emoji variation selector 
    let emojiPattern3 = "\\p{Emoji_Modifier_Base}\\p{Emoji_Modifier}" //Characters with emoji modifier 
    let emojiPattern4 = "[\\U0001F1E6-\\U0001F1FF][\\U0001F1E6-\\U0001F1FF]" //2-letter flags 
    let pattern = "\(emojiPattern4)|\(emojiPattern3)|\(emojiPattern2)|\(emojiPattern1)" 
    let combinedPattern = "(?:\(pattern))(?:\\u200D(?:\(pattern)))*" 
    let regex = try! NSRegularExpression(pattern: combinedPattern, options: []) 
    let matches = regex.matches(in: str, options: [], range: NSRange(0..<str.utf16.count)) 
    return matches.map{(str as NSString).substring(with: $0.range)} 
} 
print(emojisIncludingCombined("ab‍❤️‍‍c")) //->["", "‍❤️‍‍", ""] 

UPDATE2

這夫特2/9的iOS SDK工作的一個例子。

func emojis(str: String) -> [String] { 
    //You may need to add "refine" (or "tune") emojiPattern1...emojiPattern4. 
    let emojiPattern1 = "[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900-\\U0001F9FF]" //Code Points with default emoji representation 
    let emojiPattern2 = "[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900–\\U0001F9FF]\\uFE0F" //Characters with emoji variation selector 
    let emojiPattern3 = "[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900–\\U0001F9FF][\\U0001F3FB-\\U0001F3FF]" //Characters with emoji modifier 
    let emojiPattern4 = "[\\U0001F1E6-\\U0001F1FF][\\U0001F1E6-\\U0001F1FF]" //2-letter flags 
    let pattern = "\(emojiPattern4)|\(emojiPattern3)|\(emojiPattern2)|\(emojiPattern1)" 
    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 ?" 
let str4 = "I am going to the ⛱beach with some monkeys " 
let str5 = "Japan boy ♏️Scorpis" //="\u{1F1EF}\u{1F1F5}Japan \u{1F466}\u{1F3FB}boy \u{264f}\u{FE0F}Scorpis" 
let str6 = " xxx sss " 
let str7 = "ab‍❤️‍‍" 

print(emojis(str1)) //->["", ""] 
print(emojis(str4)) //->["⛱", "", ""] 
print(emojis(str5)) //->["", "", "♏️"] 
print(emojis(str6)) //->["", ""] 
print(emojis(str7)) //->["", "", "❤️", "", ""] 

func emojisIncludingCombined(str: String) -> [String] { 
    //You may need to add "refine" (or "tune") emojiPattern1...emojiPattern4. 
    let emojiPattern1 = "[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900-\\U0001F9FF]" //Code Points with default emoji representation 
    let emojiPattern2 = "[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900–\\U0001F9FF]\\uFE0F" //Characters with emoji variation selector 
    let emojiPattern3 = "[\\u2600-\\u27BF\\U0001F300-\\U0001F77F\\U0001F900–\\U0001F9FF][\\U0001F3FB-\\U0001F3FF]" //Characters with emoji modifier 
    let emojiPattern4 = "[\\U0001F1E6-\\U0001F1FF][\\U0001F1E6-\\U0001F1FF]" //2-letter flags 
    let pattern = "\(emojiPattern4)|\(emojiPattern3)|\(emojiPattern2)|\(emojiPattern1)" 
    let combinedPattern = "(?:\(pattern))(?:\\u200D(?:\(pattern)))*" 
    let regex = try! NSRegularExpression(pattern: combinedPattern, options: []) 
    let matches = regex.matchesInString(str, options: [], range: NSRange(0..<str.utf16.count)) 
    return matches.map{(str as NSString).substringWithRange($0.range)} 
} 

print(emojisIncludingCombined(str7)) //->["", "‍❤️‍‍"] 

如上代碼註釋,emojiPattern1 ... emojiPattern4並不代表一套完整的iOS中使用表情符號。 (包括Swift 3的代碼。)您可能需要修改模式。

+0

我需要iOS 9的支持。 –

+0

@RedMak,更新,請嘗試。 – OOPer