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

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
ketiwu
  • 685
  • 2
  • 6
  • 9

5 Answers5

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
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

filleduchaos
  • 626
  • 8
  • 7
  • thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ? – ketiwu Nov 10 '18 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
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • i'm getting bad results when replacing '.' with '' in a string of numbers "32.151" – alireza easazade Feb 16 '20 at 08:25
  • 5
    @A.easazadeh, That is because the `.` dot character means [match any single character](https://medium.com/@suragch/how-to-match-any-single-character-with-a-regular-expression-421b9fc13681) 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 '20 at 02:59
8

Removing characters "," from string:

String myString = "s, t, r";
myString = myString.replaceAll(",", ""); // myString is "s t r"
Andrei R
  • 1,433
  • 2
  • 10
  • 16
7

First solution

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

Second solution

s.replaceAll(",", "").replaceAll("!", "").replaceAll("'", "");
Hasan El-Hefnawy
  • 1,249
  • 1
  • 14
  • 20