public void GeneratePreview(Subtitle subtitle, List<ReplaceExpression> replaceRules) { var fixedSubtitle = new Subtitle(subtitle); _previewItems = new List<PreviewItem>(); FixCount = 0; var replaceExpressions = new HashSet<ReplaceExpression>(); foreach (var item in replaceRules) { if (item.Checked) { string findWhat = item.FindWhat; if (!string.IsNullOrEmpty(findWhat)) // allow space(s) { replaceExpressions.Add(item); if (item.SearchType == SearchTypeRegularExpression && !_compiledRegExList.ContainsKey(findWhat)) { _compiledRegExList.Add(findWhat, new Regex(findWhat, RegexOptions.Compiled | RegexOptions.Multiline)); } } } } foreach (Paragraph p in subtitle.Paragraphs) { bool hit = false; string newText = p.Text; foreach (ReplaceExpression item in replaceExpressions) { if (item.SearchType == SearchTypeCaseSensitive) { if (newText.Contains(item.FindWhat)) { hit = true; newText = newText.Replace(item.FindWhat, item.ReplaceWith); } } else if (item.SearchType == SearchTypeRegularExpression) { Regex r = _compiledRegExList[item.FindWhat]; if (r.IsMatch(newText)) { hit = true; newText = r.Replace(newText, item.ReplaceWith); } } else { int index = newText.IndexOf(item.FindWhat, StringComparison.OrdinalIgnoreCase); if (index >= 0) { hit = true; do { newText = newText.Remove(index, item.FindWhat.Length).Insert(index, item.ReplaceWith); index = newText.IndexOf(item.FindWhat, index + item.ReplaceWith.Length, StringComparison.OrdinalIgnoreCase); } while (index >= 0); } } } if (hit && newText != p.Text) { FixCount++; _previewItems.Add(new PreviewItem(p.ID, true, p.Number.ToString(), p.Text, newText)); int index = subtitle.GetIndex(p); fixedSubtitle.Paragraphs[index].Text = newText; } } _previewBox.Title = string.Format(Configuration.Settings.Language.MultipleReplace.LinesFoundX, FixCount); ShowPreview(_previewItems); }