Regex 如何使用正则表达式解决dart中的单词计数问题?
做一个练习,试着得到所有的案例 给定一个短语,计算该短语中每个单词的出现次数 例如输入“olly-olly-in-come-free” 到目前为止,我得到的是:Regex 如何使用正则表达式解决dart中的单词计数问题?,regex,dart,Regex,Dart,做一个练习,试着得到所有的案例 给定一个短语,计算该短语中每个单词的出现次数 例如输入“olly-olly-in-come-free” 到目前为止,我得到的是: Map<String, int> countWords(String words) { var wordCount = Map<String, int>(); words.split(RegExp(r"\W")).forEach( (word) { wordCount.updat
Map<String, int> countWords(String words) {
var wordCount = Map<String, int>();
words.split(RegExp(r"\W")).forEach(
(word) {
wordCount.update(word.toLowerCase(), (value) => value + 1,
ifAbsent: () => 1);
},
);
return wordCount;}
我不擅长正则表达式,所以我不确定我错过了什么。请帮助我找到解决方案,谢谢。不要使用每个非单词字符拆分字符串,只需获取单词:
var-wordCount={};
for(RegExp(r“\w+('\w+)?)中的变量匹配)。allMatches('my&string with with symbols&%!1')){
更新(match.group(0).toLowerCase(),(value)=>value+1,
如果有:()=>1);
}
打印(字数);
这是一个非常重要的问题,可能无法用半行的正则表达式解决。您好,谢谢您的回答。此解决方案很接近,但适用于“转义”的3种情况。比如“乔分不清应用程序、苹果和a”,“乔分不清‘大’和‘大’”,以及“第一:不要笑”。然后:不要哭。我需要添加什么来包含它?是否要将不能
作为单个单词进行匹配?而的
是否应算作一个不同的词(如Mattia和Mattia的)?是的。像Mattia's、don't、cant等应该是一个单词。下面是测试“预期:{joe':1,'can't':1,'tell':1,'between':1,'large':2,'and':1}的输出示例实际:{joe':1,'can':1,'t':1,'tell':1,'between':1,'large':2,'and':1},其:长度不同且缺少映射键“can't'”。我已经更新了表达式,请再次尝试获取帮助,效果良好。
Map<String, int> countWords(String words) {
var wordCount = Map<String, int>();
words.split(RegExp(r"\W")).forEach(
(word) {
wordCount.update(word.toLowerCase(), (value) => value + 1,
ifAbsent: () => 1);
},
);
return wordCount;}
'one,\ntwo,\nthree'
'car: carpet as java: javascript!!&@\$%^&'
'Joe can\'t tell between \'large\' and large.'
'testing, 1, 2 testing'
'First: don\'t laugh. Then: don\'t cry.'
'Joe can\'t tell between app, apple and a.'
' multiple whitespaces'
',\n,one,\n ,two \n \'three\''