In Dart language how can i remove all brackets which i have integers between them, for example in this below string i want to remove [1] and [100]. integers of between brackets aren't specific

with this code i can remove all brackets but i can't remove all integers

String s = "Hello, world![1] i am 'foo' [100]";
print(s.replaceAll(new RegExp(r'(?:_|[^\w\s])+'),''));

output:

Hello, world!1 i am 'foo' 100

that should be:

Hello, world! i am 'foo'


Solution 1: Jahidul Islam

you can try this

print(s.replaceAll(new RegExp(r'([^\D])+'),'').replaceAll("[]",''));

Output - Hello, world! i am 'foo'


Solution 2: Wiktor Stribiżew

You can use

String s = "Hello, world![1] i am 'foo' [100]";
print(s.replaceAll(new RegExp(r'\s*\[\d+]'),''));
// => Hello, world! i am 'foo'

The \s*\[\d+] regex matches

  • \s* - zero or more whitespaces
  • \[ - a [ char
  • \d+ - one or more digits
  • ] - a ] char.

See the regex demo.