private void Listbox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (_lists.Count < 1) return; var content = ReplaceTb.Text; Task.Factory.StartNew(() => { content = _regex.Compiler(content); Dispatcher.Invoke(() => { var chooseWindow = new ChooseWindow {Content = content}; chooseWindow.Show(); }); }); }
private void Listbox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { int sum = Listbox.Items.Count; int index = Listbox.SelectedIndex; CountLb.Text = "当前状态:"+(index+1).ToString(CultureInfo.InvariantCulture)+"/"+sum.ToString(CultureInfo.InvariantCulture); if (index < 0 || index >= sum) return; string temp = ReplaceTb.Text; if (temp.IndexOf("$foreach", System.StringComparison.Ordinal)>=0) { Match m= Regex.Match(temp, @"^(\$foreach)(?<Text>.*?)(end\$)$"); string temp1 = m.Groups["Text"].Value; string text = Listbox.Items.Cast<object>().Aggregate("", (current, t) => current + (temp1.Replace("$$", t.ToString()) + "\n")); temp = temp.Replace(m.Value, text); } else if (temp.IndexOf("$for", System.StringComparison.Ordinal) >=0) { Match m = Regex.Match(temp, @"^(\$for)(?<Text>[\s\S]*?)(end\$)$"); //正则表达式匹配任意字符(包括换行符)的写法:([\s\S]*) 同时,也可以用 “([\d\D]*)”、“([\w\W]*)” 来表示。 string temp1 = m.Groups["Text"].Value; int count= Regex.Matches(temp1, @"\$\$").Count; if (count==sum) { Regex reg = new Regex(@"\$\$"); for (int i = 0; i < count; i++) { temp1=reg.Replace(temp1,Listbox.Items[i].ToString(),1); } temp = temp.Replace(m.Value, temp1); } else { MessageBox.Show("需要替换的地方有"+count.ToString(CultureInfo.InvariantCulture)+"个\n列表中有"+sum.ToString(CultureInfo.InvariantCulture)+"个", "提示"); } } else { temp = temp.Replace("$$", Listbox.Items[index].ToString()); } ChooseWindow chooseWindow = new ChooseWindow {Content = temp}; chooseWindow.Show(); }