在飞镖中获取以符号开头的单词

Getting words Starting with symbol in dart

我正在尝试在 Dart 中解析包含主题标签的长字符串,到目前为止,我尝试了各种与正则表达式的组合,但我找不到正确的用法。

我的密码是

String mytestString = "#one #two, #three#FOur,#five";
RegExp regExp = new RegExp(r"/(^|\s)#\w+/g");

print(regExp.allMatches(mytestString).toString());

期望的输出将是一个 hahstags 列表

#one #two #three #FOur #five

提前致谢

String mytestString = "#one #two, #three#FOur,#five";
RegExp regExp = new RegExp(r"/(#\w+)/g");

print(regExp.allMatches(mytestString).toString());

这应该匹配所有主题标签,将它们放入捕获组中供您以后使用。

您不应在字符串文字中使用正则表达式文字,否则反斜杠和标志将成为正则表达式 模式 的一部分。此外,如果您需要在任何上下文中匹配 # 后跟 1+ 个单词字符,请省略左侧边界模式(匹配字符串或空格的开头)。

使用

String mytestString = "#one #two, #three#FOur,#five";
final regExp = new RegExp(r"#\w+");
Iterable<String> matches = regExp.allMatches(mytestString).map((m) => m[0]);
print(matches);

输出:(#one, #two, #three, #FOur, #five)