111,125
社区成员
发帖
与我相关
我的任务
分享又是正则
using System.Text.RegularExpressions;
private int GetCount(string text,string keyWord)
{
Regex reg=new Regex(keyWord);
return reg.Matchs(text).Length;
}
string text = "aaabbbcccaaabbbcccaaabbbcccaaacccaaaddd";
string pattern = @"(.)\1*";
Dictionary<string, int> dic = new Dictionary<string, int>();
Regex.Replace(text, pattern, delegate(Match m)
{
if (dic.ContainsKey(m.Value))
{
dic[m.Value]++;
}
else
{
dic.Add(m.Value, 1);
}
return null;
});
foreach (string key in dic.Keys)
{
Console.WriteLine(key + ": " + dic[key]);
}