Ad

How To Get Delimiters After Split By Multiple Delimiters?

- 1 answer

I use this phones = value.split(RegExp(r":| |:| |)")); to split my phone numbers, so I got 内線, 7+90+21931465 and 070-2193-1465 from 内線:7+90+21931465:070-2193-1465, but I want to show them as 内線:7+90+21931465:070-2193-1465, how can I get array as 内線, , 7+90+21931465, : and 070-2193-1465?

Ad

Answer

  var value = '内線:7+90+21931465:070-2193-1465';
  var regExp = RegExp(r'^(.+)(:|:)(.+)(:|:)(.+)$');

  print(value);
  var match = regExp.firstMatch(value);
  var numGroups = match.groupCount;
  var groups = match.groups(List<int>.generate(numGroups, (i) => i + 1));
  print(groups.toString());

Another way:

  var chesu = value.split(RegExp(r':|:'))..insert(1, ':')..insert(3, ':');
  print(chesu);
Ad
source: stackoverflow.com
Ad