我必须解析一些字符串并提取表情符号。
我找不到任何好的解决方案。
假设我有这个字符串:
“xxx sss”
我怎样才能获得这2个表情符号?
请注意:“== +(Emoji Modifier Fitzpatrick Type-6)”
答案 0 :(得分: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 ")) //->["", ""]
(更新)删除了上述代码中的一些错误。并尝试检测一些组合的表情符号。
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 强>
一个适用于Swift 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的代码。)您可能需要修改模式。