【问题标题】:How do I extract emojis from a string in Flutter?如何从 Flutter 中的字符串中提取表情符号?
【发布时间】:2021-06-05 11:08:02
【问题描述】:

我需要从字符串中提取表情符号,用于我在 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");
}

【问题讨论】:

  • 请添加您目前使用的代码以查看问题所在。
  • @TamirAbutbul 我试图使用该链接中的解决方案,但它似乎对我不起作用
  • @WiktorStribiżew 我已经添加了到目前为止我尝试过的代码
  • @WiktorStribiżew 不想删除表情符号,我只想从字符串中删除除表情符号之外的所有其他内容

标签: android regex flutter dart


【解决方案1】:

你可以使用

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?,它将表情符号的适当和浅色皮肤与深色皮肤模式字符以及红发到白发字符匹配。

【讨论】:

  • 谢谢@Wiktor Stribiżew,这似乎对我有用,但是我看到这个警告“使用有效的正则表达式语法。”,知道如何解决这个问题吗?
  • @AravindKarthik 这看起来像是Linter for Dart 发出的警告。不确定它是否会在那里修复,或者是否可以停用警告。
  • 这个正则表达式比这篇文章中推荐的另一个更好。比如对方没接❤️,有点意外。
猜你喜欢
  • 1970-01-01
  • 2017-12-31
  • 2018-12-13
  • 2021-09-21
  • 2016-05-26
  • 2018-08-22
  • 2022-06-22
  • 2014-04-06
  • 1970-01-01
相关资源
最近更新 更多