splitWithContent method

List<String> splitWithContent(
  1. Pattern pattern
)

Implementation

List<String> splitWithContent(Pattern pattern) {
  var parts = split(pattern);
  var matches = pattern.allMatches(this).toList();
  var count = parts.length + matches.length;

  var result = <String>[];

  for (var i in range(end: count)) {
    if (i % 2 == 0) {
      var j = i ~/ 2;
      result.add(parts[j]);
    } else {
      var j = (i - 1) ~/ 2;
      var match = matches[j];
      var matchContent = match
          .groups(range(end: match.groupCount + 1, start: 1))
          .where((e) => e != null)
          .cast<String>()
          .toList();
      result.addAll(matchContent);
    }
  }

  return result;
}