Home > Mobile >  Howto do this in Dartt $current_segment =~ s/([a-zA-Z] )\.([a-zA-Z] )/$1 punto $2/g;
Howto do this in Dartt $current_segment =~ s/([a-zA-Z] )\.([a-zA-Z] )/$1 punto $2/g;

Time:06-27

I do appreciate if anyone could help me with this seemingly easy question:

$someText =~ s/([a-zA-Z] )\.([a-zA-Z] )/$1 : $2/g;

This perl statement finds all patterns of the form www.xyz and changes them to www:xyz. I need an equivalent code in Dart; I am new to Dart. Thanks, Bid

CodePudding user response:

You could do it something like this:

RegExp regExp = RegExp(r'([a-zA-Z] )\.([a-zA-Z] )');

void main() {
  String someText = 'www.xyz';
  someText = someText.replaceAllMapped(regExp, (m) => '${m[1]}:${m[2]}');
  print(someText); // www:xyz
}

I have put the RegExp outside main to illustrate it is best to keep this object around, instead of recreating the RegExp for every usage, since it can be expensive, in runtime, depending of how it is used.

  • Related