This code works only in the latest iOS 10 SDK, and cannot detect some combined emojis, but if that's OK with you, please try:
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
Removed some errors in above code. And a little try to detect some combined 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("a👍🏼b👨❤️💋👨c🇯🇵")) //->["👍🏼", "👨❤️💋👨", "🇯🇵"]
UPDATE2
An example which works in 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 = "a👍🏼b👨❤️💋👨"
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)) //->["👍🏼", "👨❤️💋👨"]
As commented in the code, emojiPattern1...emojiPattern4 are not representing a complete set of emojis available in iOS. (Including the codes for Swift 3.) You may need to modify the patterns.
From:
网友评论