stringdartsplitpunctuation

Split a string into an array of words, punctuation and spaces in Dart


I'm trying to replicate a method mentioned on this page:

Split a string into an array of words, punctuation and spaces in JavaScript

For example:

var text = "I like grumpy cats. Do you?";
console.log(
  text.match(/\w+|\s+|[^\s\w]+/g)
)

Returns:

[
  "I",
  " ",
  "like",
  " ",
  "grumpy",
  " ",
  "cats",
  ".",
  " ",
  "Do",
  " ",
  "you",
  "?"
]

But instead of Javascript, I'm using Dart. I'm having a hard time finding examples of how this would work in Dart, especially in formatting the regex.

I've tried this, but it's not returning the punctuation and spaces:

dynamic textToWords(String text) {
  // Get an array of words, spaces, and punctuation for a given string of text.
  var re = RegExp(r"\w+|\s+|[^\s\w]+g");
  final words = text != null
      ? re.allMatches(text != null ? text : '').map((m) => m.group(0)).toList()
      : [];
  return words;
}

Any help is appreciated.


Solution

  • Remove the g from the end of your RegExp.

    Also text will never be null since you declared it as a String, so there is no need for these null checks.

    List<String> textToWords(String text) {
      // Get an array of words, spaces, and punctuation for a given string of text.
      var re = RegExp(r"\w+|\s+|[^\s\w]+");
      final words = re.allMatches(text).map((m) => m.group(0) ?? '').toList();
      return words;
    }