Home > Software design >  I want to remove symbols from a string in dart
I want to remove symbols from a string in dart

Time:08-24

I want to remove all symbols except for characters (Japanese hiragana, kanji, and Roman alphabet ) that unmatch this regex.

var reg = RegExp(
                r'([\u3040-\u309F]|\u3000|[\u30A1-\u30FC]|[\u4E00-\u9FFF]|[a-zA-Z]|[々〇〻])');

I don't know what to put in this "?".

text=text.replaceAll(?,"");
a="「私は、アメリカに行きました。」、'I went to the United States.'"

b="私はアメリカに行きましたI went to the United States"

I want to make a into b.

CodePudding user response:

You can use

String a = "「私は、アメリカに行きました。」、'I went to the United States.'";
a = a.replaceAll(RegExp(r'[^\p{L}\p{M}\p{N}\s] ', unicode: true), '') );

Also, if you just want to remove any punctuation or math symbols, you can use

.replaceAll(RegExp(r'[\p{P}\p{S}] ', unicode: true), '')

Output:

私はアメリカに行きましたI went to the United States

The [^\p{L}\p{M}\p{N}\s] regex matches one or more chars other than letters (\p{L}), diacritics (\p{M}), digits (\p{N}) and whitespace chars (\s).

The [\p{P}\p{S}] regex matches one or more punctuation proper (\p{P}) or match symbol (\p{S}) chars.

The unicode: true enables the Unicode property class support in the regex.

CodePudding user response:

You can need to specify the Pattern (RegEx) you want to apply on your replaceAll method.

// Creating the regEx/Pattern
var reg = RegExp(r'([\u3040-\u309F]|\u3000|[\u30A1-\u30FC]|[\u4E00-\u9FFF]|[a-zA-Z]|[々〇〻])');

// Applying it to your text.
text=text.replaceAll(reg,"");

You can learn more about it here:
https://api.flutter.dev/flutter/dart-core/String/replaceAll.html

  • Related