在 Dart 中将字符串拆分为单词、标点符号和空格的数组

发布于 2025-01-10 20:48:54 字数 913 浏览 0 评论 0原文

我正在尝试复制此页面上提到的方法:

在 JavaScript 中将字符串拆分为单词、标点符号和空格的数组

例如:

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

返回:

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

但我使用的不是 Javascript,而是 Dart。我很难找到在 Dart 中如何工作的示例,尤其是在格式化正则表达式方面。

我已经尝试过此操作,但它没有返回标点符号和空格:

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;
}

感谢任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2025-01-17 20:48:54

RegExp 末尾删除 g

而且 text 永远不会为 null,因为您将其声明为 String,因此不需要这些 null 检查。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文