2

I need to extract emojis from a string for this side project I am working on in Flutter

Input: "Hey everyone "

Output: ""

How do I achieve this?

This is what I have tried so far based on this post

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");
}

1 Answers1

4

You can use

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 "));
}

Output:

The [\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\u{1F9B0}-\u{1F9B3}] regex is taken from Why do Unicode emoji property escapes match numbers? and it matches emojis proper and light skin to dark skin mode chars and red-haired to white-haired chars.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you @Wiktor Stribiżew, this seems to be working for me however I am seeing this warning "Use valid regular expression syntax.", any idea how to fix this? – Aravind Karthik Jun 05 '21 at 12:55
  • @AravindKarthik That looks like a warning produced by [Linter for Dart](https://dart-lang.github.io/linter/lints/valid_regexps.html). Not sure if it is going to be fixed there, or if it is possible to deactivate the warning. – Wiktor Stribiżew Jun 05 '21 at 19:31
  • 1
    This regex works better than the other recommended in this post. For example, the other one didn't pick up on ❤️, which was a bit surprising. – Sean Aug 27 '21 at 14:51