如何在包含方括号的字符串中使用拆分方法?

时间:2019-08-07 05:49:47

标签: flutter dart

我有一个包含一些数据的字符串。数据是这样分离的:

 var stringData = (SomeWordsWithSpacesInBetween) 0 (SomeWordsWithSpaceInBetween) 1 ...

我希望能够提取方括号之间的数据以及方括号中的单词之间的数字,例如:

stringData.split( some way to split them)[0] = SomeWordsWithSpacesInBetween;
stringData.split(some way to split them)[1] = 0;

如何以这种方式拆分它们?

2 个答案:

答案 0 :(得分:1)

var s = '(Some Words With Spaces InBetween) 0 (SomeWordsWithSpaceInBetween) 1';
var r = RegExp(r'\(((\w+ ?)*)\) (\d+) ?').allMatches(s).expand((e) => [e[1], e[3]]);

答案 1 :(得分:0)

您可以使用正则表达式来实现。这是一个例子。

List<String>getStringList(){

  String abc = '(SomeWordsWithSpacesInBetween) 0 (SomeWordsWithSpaceInBetween) 1 (SomeWordsWithSpaceInBetween)';
  List<String> myList = new List();
  RegExp exp = new RegExp(r"\) (\d+) \(");

  myList = abc.split(exp);
  print('${myList}');

  return myList;
}