46

I want to remove all special symbols from string and have only words in string I tried this but it gives same output only

main() {
    String s = "Hello, world! i am 'foo'";
    print(s.replaceAll(new RegExp('\W+'),'')); 
}

output : Hello, world! i am 'foo'
expected : Hello world i am foo

5 Answers 5

55

There are two issues:

  • '\W' is not a valid escape sequence, to define a backslash in a regular string literal, you need to use \\, or use a raw string literal (r'...')
  • \W regex pattern matches any char that is not a word char including whitespace, you need to use a negated character class with word and whitespace classes, [^\w\s].

Use

void main() {
  String s = "Hello, world! i am 'foo'";
  print(s.replaceAll(new RegExp(r'[^\w\s]+'),''));
}

Output: Hello world i am foo.

Fully Unicode-aware solution

Based on What's the correct regex range for javascript's regexes to match all the non word characters in any script? post, bearing in mind that \w in Unicode aware regex is equal to [\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}], you can use the following in Dart:

void main() {
  String s = "Hęllo, wórld! i am 'foo'";
  String regex = r'[^\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}\s]+';
  print(s.replaceAll(RegExp(regex, unicode: true),''));
}
// => Hęllo wórld i am foo
6
  • 1
    worked and additionaly ive added .replaceall("_","") at the end
    – Rajesh
    Feb 12, 2019 at 13:24
  • 4
    @RajeshJr. You may merge that into 1 regex: .replaceAll(new RegExp(r'(?:_|[^\w\s])+', '') Feb 12, 2019 at 13:26
  • Simple but it will replace non-English 'LETTERS' as well
    – Ehab Reda
    Dec 25, 2021 at 2:49
  • 1
    @EhabReda Updated with a fully Unicode aware solution. Dec 25, 2021 at 13:11
  • 1
    @SarahK These are matched with \s. If you want to only keep regular spaces (\x20), then simply replace \s with a regular space, i.e. [^\w ]+ Aug 19 at 7:52
21

The docs for the RegExp class state that you should use raw strings (a string literal prefixed with an r, like r"Hello world") if you're constructing a regular expression that way. This is particularly necessary where you're using escapes.

In addition, your regex is going to catch spaces as well, so you'll need to modify that. You can use RegExp(r"[^\s\w]") instead - that matches any character that's not whitespace or a word character

1
  • thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
    – ketiwu
    Nov 10, 2018 at 15:32
21

I found this question looking for how to remove a symbol from a string. For others who come here wanting to do that:

final myString = 'abc=';
final withoutEquals = myString.replaceAll(RegExp('='), ''); // abc
2
  • i'm getting bad results when replacing '.' with '' in a string of numbers "32.151" Feb 16, 2020 at 8:25
  • 5
    @A.easazadeh, That is because the . dot character means match any single character in regular expressions. Thus, in your example every single character is matched and replaced. If you only want to match the literal . dot, then you need to escape it as RegExp('\\.') or as RegExp(r'\.') or as RegExp('[.]').
    – Suragch
    Feb 17, 2020 at 2:59
8

Removing characters "," from string:

String myString = "s, t, r";
myString = myString.replaceAll(",", ""); // myString is "s t r"
7

First solution

s.replaceAll(RegExp(",|!|'"), "");    // The | operator works as OR

Second solution

s.replaceAll(",", "").replaceAll("!", "").replaceAll("'", "");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.