how can I use regex to replace multiple characters in string at once

3,158 views
Skip to first unread message

Niyazi Toros

unread,
Mar 31, 2019, 3:59:21 AM3/31/19
to Dart Misc

In dart how can I use regex to replace multiple characters in string at once.

Example as shown below I need to replace the _orignName characters to output characters.

String _orignName = "çÇğĞıİşŞöÖüÜ";
output: "cCgGiIsSoOuU";

Lasse Reichstein Holst Nielsen

unread,
Apr 1, 2019, 1:26:19 AM4/1/19
to Dart Misc
A RegExp does not replace anything, it just matches. You can use a RegExp to match the characters you need to replace, and then use `String.replaceAllMapped` to find those characters in a string and replace them with something else.

I'd do it as:

final matchRE = RegExp(r"[çÇğĞıİşŞöÖüÜ]");
final replacementMap = Map.fromIterables("çÇğĞıİşŞöÖüÜ".split(""), "cCgGiIsSoOuU".split(""));

String replaceMyChars(String input) => 
    input.replaceAllMapped(matchRE, (m) => replacementMap[m[0]]);
 
/L

Man Hoang

unread,
Apr 1, 2019, 5:29:47 AM4/1/19
to Dart Misc
Are you trying to latinize (remove the diacritics from) the string? If so, please check out this package https://pub.dartlang.org/packages/diacritic

Niyazi Toros

unread,
Apr 1, 2019, 6:24:40 AM4/1/19
to Dart Misc
Thanks, I implement the diacritic to overcome my problem but unfortunatelyI have to write some manual codding to convert IBM CP850 to Latin and still having problem. 

The Dart and Flutter uses Unicode çÇğĞıİşŞöÖüÜ characters and their byte value is different than IBM CP850. Still working and trying to find a solution.
Reply all
Reply to author
Forward
0 new messages