我需要从字符串中提取表情符号,用于我正在 Flutter 中开展的副项目
输入: “大家好🥺🥺🥺”
输出: “🥺🥺🥺”
如何实现这一目标?
这是我迄今为止根据 this 尝试过的发布
var emojiRegex = RegExp(
"r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])'");
getEmojis(String message) {
print("EXTRACTING EMOJI");
var output = message.replaceAll(emojiRegex,"");
print("EMOJI: $output");
}
最佳答案
你可以使用
String extractEmojis(String text) {
RegExp rx = RegExp(r'[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]', unicode: true);
return rx.allMatches(text).map((z) => z.group(0)).toList().join("");
}
void main() {
print(extractEmojis("Hey everyone 🥺🥺🥺"));
}
输出:🥺🥺🥺
[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}]
正则表达式取自 Why do Unicode emoji property escapes match numbers?它可以将表情符号正确匹配,浅色皮肤与深色皮肤模式字符以及红发与白发字符相匹配。
关于android - 如何从 Flutter 中的字符串中提取表情符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67848924/