2016-08-24 10 views
2

私はいくつかの文字列を解析し、emojisを抽出する必要があります。NSStringからemoji(unicode)を抽出する

私はそれを行う良い解決策を見つけることができません。

レッツは、私は、この文字列を持っていることを言う:

"XXX SSS"

どのように私はこれらの2個の絵文字を得ることができますか?

なお:「== +(絵文字修飾子フィッツパトリックタイプ-6)」

+0

Regexを試すことができます –

+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で動作し、いくつかの組み合わせ絵文字を検出することはできませんが、それはあなたとOKだ場合には、してください試してみてください。

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 ")) //->["", ""] 

(UPDATE)は、上記のコードでいくつかのエラーを削除しました。いくつかの組み合わせの絵文字を検出しようとします。

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")) //->["", "‍❤️‍‍", ""] 

アップデート2

スイフト2/IOSの9 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

関連する問題