searching and extract group of string from my String:
my string
hello, my name is dev and ..., OK :#E8J+Yng==#::#E8J+Yng==#:, and i just:#E8J+YjQ==#:,:#E8J+YsA==#:

and the original string is hello, my name is dev and ..., OK 😞😞, and i just😍,😰 I get this String by the following class

import 'dart:convert' as converter;

class EmojiMessages{
  static String regStr=r"(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])";
  static RegExp regExp=RegExp(regStr);
  static String startEmoji=":#E";
  static String endEmoji="#:";

  static String encodeEmojiText(String msg){
    List<String> emojis=regExp.allMatches(msg).map((e)=>e.group(0)).toList();
    List<String> codedEmojis=emojis.map((e)=>startEmoji+converter.base64.encode(converter.utf8.encode(e))+endEmoji).toList();
    for(var i=0; i<emojis.length; i++){
      msg=msg.replaceAll(emojis[i], codedEmojis[i]);
    }
    return msg;
  }

  static String decodeEmojiText(String msg){
    //todo revers emoji base64
    return msg;
  }
}

var str="hello, my name is dev and ..., OK 😞😞, and i just😍,😰";
print(EmojiMessages.encodeEmojiText(str));//hello, my name is dev and ..., OK :#E8J+Yng==#::#E8J+Yng==#:, and i just:#E8J+YjQ==#:,:#E8J+YsA==#:

What I Need, just revers top code like regexp to select :#Ebase64#:
to get original string again.

why I am doing this?

to store data without spacial character, In My API It's Required to Send Normal Message String

  • and please:
    • if the encode base64 way is false, TELL ME.
    • if the encode base64 way is unreversable, TELL ME.
    • be patient with me, I weak on RegExp.

thank youe ThaiKV


Solution 1: Wiktor StribiΕΌew

You can choose whatever type of plain text representation to keep your Unicode data safe upon conversion to ASCII, Base64 is a safe format.

The way you wrote the encoding method is not optimal, you should just use .replaceAllMapped when you need to match substrings with regex, manipulate them and then paste back into the string. Extracting the matches first, converting the data and running replace after each manipulation is resource consuming.

So, I'd write your EmojiMessages class as follows:

class EmojiMessages{
  static String regStr=r"(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])";
  static String startEmoji=":#E";
  static String endEmoji="#:";
  static RegExp regExp=RegExp(regStr);
  static RegExp regExp_dec_emoji = new RegExp("$startEmoji(.*?)$endEmoji");

  static String encodeEmojiText(String msg){
    return msg.replaceAllMapped(regExp, 
        (Match m) => "$startEmoji${converter.base64.encode(converter.utf8.encode(m[0]))}$endEmoji");
  }
  static String decodeEmojiText(String msg){
    return msg.replaceAllMapped(regExp_dec_emoji, 
        (Match m) => converter.utf8.decode(converter.base64.decode(m[1])));
  }
}

Test:

var str="hello, my name is dev and ..., OK 😞😞, and i just😍,😰";
print(str);
// => hello, my name is dev and ..., OK 😞😞, and i just😍,😰
String encstr = EmojiMessages.encodeEmojiText(str);
print(encstr);
// => hello, my name is dev and ..., OK :#E8J+Yng==#::#E8J+Yng==#:, and i just:#E8J+YjQ==#:,:#E8J+YsA==#:
print(EmojiMessages.decodeEmojiText(encstr));
// => hello, my name is dev and ..., OK 😞😞, and i just😍,😰

NOTES:

  • regExp_dec_emoji is a simple regex capturing any kind of zero or more chars (other than line break chars) between two strings, :#E(.*?)#:
  • converter.utf8.decode(converter.base64.decode(m[1])) is used to decode Group 1 value, the string between :#E and the leftmost #:.