我正在尝试在Dart中解析包含标签的长字符串,到目前为止,我尝试使用regexp进行各种组合,但找不到正确的用法。
我的代码是
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
先谢谢您
答案 0 :(得分:2)
请勿在字符串文字中使用正则表达式文字,否则反斜杠和标志将成为正则表达式 pattern 的一部分。另外,如果需要在任何上下文中匹配#
后跟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)
答案 1 :(得分:1)
String mytestString = "#one #two, #three#FOur,#five";
RegExp regExp = new RegExp(r"/(#\w+)/g");
print(regExp.allMatches(mytestString).toString());
这应该与所有主题标签匹配,将它们放在捕获组中,以供以后使用。