Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

regex - How to get delimiters after split by multiple delimiters?

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...