如何将任何语言编写的字符串拆分为单词(使用 Flutter/Dart)?

问题描述 投票:0回答:1

我有这个字符串:

I like grumpy cats. Do you? ффффф ыыыыы ইউটিউব থেকে

如果我在正则表达式中使用

\w
- 那么我只能得到用拉丁字母写的单词:

  final text = "I like grumpy cats. Do you? ффффф ыыыыы ইউটিউব থেকে";
  RegExp re = RegExp(r"\w+");
  List<String> words = [];
  for (Match match in re.allMatches(text)) {
    words.add(match.group(0)!);
  }
  print(words);

输出:

[I, like, grumpy, cats, Do, you]

但我需要这个结果:

[I, like, grumpy, cats, Do, you, ффффф, ыыыыы, ইউটিউব, থেকে]

this答案中,我发现

\p{L}
的意思是“来自任何语言的任何类型的字母”。但我无法让它在 Flutter/Dart 中工作

regex flutter dart
1个回答
0
投票

您可以将

\w
简写字符类分解为其组成的 Unicode 类别类,还添加变音符号类,并在 RegExp 构造函数中使用
uncode: true
参数:

String text = "I like grumpy cats. Do you? ффффф ыыыыы ইউটিউব থেকে";
RegExp re = new RegExp re = new RegExp(r'[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}\p{M}]+', unicode: true);
List<String?> words = re.allMatches(text).map((z) => z.group(0)).toList();
print(words);

输出:

[I, like, grumpy, cats, Do, you, ффффф, ыыыыы, ইউটিউব, থেকে]
© www.soinside.com 2019 - 2024. All rights reserved.