public static Dictionary <string, int> FindLongestWord(string text, int length) { Dictionary <string, int> countWords = new Dictionary <string, int>(); Regex regex = new Regex("\\b\\w{" + length + ",}\\b"); foreach (Match ItemMatch in regex.Matches(text)) { string item = ItemMatch.ToString(); if (countWords.ContainsKey(item)) { countWords[item]++; } else { countWords.Add(item, 1); } } countWords.SaveTenLargestValues(); return(countWords); }
protected void btnExecute_Click(object sender, EventArgs e) { try { object oRetVal = CompileAndRunCode(this.fctb.Text); //pass compile error to command textbox rtxtOutput.Text = oRetVal.ToString(); //pass all stripped variable name from compile error to javascript to get highlighted on editor Regex ItemRegex = new Regex("(?<=\')[\\w]+(?!=\')", RegexOptions.Compiled); foreach (Match ItemMatch in ItemRegex.Matches(oRetVal.ToString())) { if (ItemMatch.Success) { string stripvalue = ItemMatch.ToString(); varlist.Add(stripvalue); txtValue.Value = ArrayListToString(ref varlist); errorJS.Append(stripvalue); errorJS.Append("|"); } } //change font color on error if (errorJS.Length != 0) { rtxtOutput.ForeColor = ColorTranslator.FromHtml("#f0471c"); } else { rtxtOutput.ForeColor = ColorTranslator.FromHtml("#000000"); } } catch (Exception x) { rtxtOutput.ForeColor = ColorTranslator.FromHtml("#f0471c"); rtxtOutput.Text = x.Message; } }
public static string ReplaceTokens(string value) { var result = value; var itemRegex = new Regex(@"\$\{(\w+)\}"); foreach (Match ItemMatch in itemRegex.Matches(value)) { var token = ItemMatch.Groups[1].Value; if (localVariables.ContainsKey(token)) { result = result.Replace(ItemMatch.ToString(), localVariables[token]); } if (token == "Clipboard") { result = result.Replace(ItemMatch.ToString(), Clipboard.GetText()); } } //${UserIndex} //${Random(1,10)} itemRegex = new Regex(@"\$\{Random\((\d+)\,(\d+)\)\}"); foreach (Match ItemMatch in itemRegex.Matches(result)) { var randFrom = ItemMatch.Groups[1].Value; var randTo = ItemMatch.Groups[2].Value; var rnd = new Random(); var randVal = rnd.Next(int.Parse(randFrom), int.Parse(randTo)); result = result.Replace(ItemMatch.ToString(), randVal.ToString()); } //${RandomExcept(1,10,5)} itemRegex = new Regex(@"\$\{RandomExcept\((\d+)\,(\d+)\,(\d+)\)\}"); foreach (Match ItemMatch in itemRegex.Matches(result)) { var randFrom = ItemMatch.Groups[1].Value; var randTo = ItemMatch.Groups[2].Value; var randExcept = int.Parse(ItemMatch.Groups[3].Value); var rnd = new Random(); var randVal = rnd.Next(int.Parse(randFrom), int.Parse(randTo)); while (randVal == randExcept) { randVal = rnd.Next(int.Parse(randFrom), int.Parse(randTo)); } result = result.Replace(ItemMatch.ToString(), randVal.ToString()); } //${RandomText(ali,veli,kirkk doggkuz)} itemRegex = new Regex(@"\$\{RandomText\(([^\)]*)\)\}"); foreach (Match ItemMatch in itemRegex.Matches(result)) { var randStrs = ItemMatch.Groups[1].Value; var items = randStrs.Split(new char[] { ',' }, StringSplitOptions.None); if (items.Any()) { var rnd = new Random(); var randVal = rnd.Next(0, items.Length); result = result.Replace(ItemMatch.ToString(), items[randVal]); } } //${CurrentDate(mmDDyyyy)} itemRegex = new Regex(@"\$\{CurrentDate\(([^\)]*)\)\}"); foreach (Match ItemMatch in itemRegex.Matches(result)) { var format = ItemMatch.Groups[1].Value; result = result.Replace(ItemMatch.ToString(), DateTime.Now.ToString(format)); } //${Eval(3+5*2)} itemRegex = new Regex(@"\$\{Eval\(([^\)]*)\)\}"); foreach (Match ItemMatch in itemRegex.Matches(result)) { var expr = ItemMatch.Groups[1].Value; DataTable dt = new DataTable(); var evalRes = dt.Compute(expr, ""); result = result.Replace(ItemMatch.ToString(), evalRes.ToString()); } //${User0Name} itemRegex = new Regex(@"\$\{User(\d+)(\w+)\}"); foreach (Match ItemMatch in itemRegex.Matches(result)) { var index = int.Parse(ItemMatch.Groups[1].Value); var prop = ItemMatch.Groups[2].Value; if (Users.ContainsKey(index)) { var propDict = new Dictionary <string, string>(); Users[index].FillToDictionary(propDict); if (propDict.ContainsKey("User" + prop)) { result = result.Replace(ItemMatch.ToString(), propDict["User" + prop]); } } } return(result); }