Home > OS >  Dart remove all brackets with integers between them
Dart remove all brackets with integers between them

Time:09-21

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'

CodePudding user response:

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.

CodePudding user response:

you can try this

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

Output - Hello, world! i am 'foo'

Generic form as @Wiktor mentioned

print(s.replaceAll(new RegExp(r'\s*\[\d ]'),''));
  • Related