public bool Replace(BackgroundWorker worker, RegexTesterPageViewModel viewModel, RegexProcessContext context) { if (!RegexIsMatch(context)) { return OperationIsComplete(worker); } int count = 0; string result = context.MatchRegex.Replace(context.InputText, delegate(Match m) { string value = context.ReplaceRegexPattern; foreach (var groupName in context.MatchRegex.GetGroupNames()) { if (worker.CancellationPending) return string.Empty; value = value.Replace(string.Format("${{{0}}}", groupName), m.Groups[groupName].Value); } count++; return value; }); viewModel.Count = count; viewModel.AppendOutputText(result); return OperationIsComplete(worker); }
public bool Split(BackgroundWorker worker, RegexTesterPageViewModel viewModel, RegexProcessContext context) { viewModel.AppendOutputText("string pattern = @\"" + StringFormat(context.MatchRegex.ToString()) + "\";"); viewModel.AppendOutputText("RegexOptions regexOptions = " + GetRegexOptions(context.MatchRegex.Options) + ";"); viewModel.AppendOutputText("Regex regex = new Regex(pattern, regexOptions);"); viewModel.AppendOutputText("string inputData = @\"" + StringFormat(context.InputText) + "\";"); viewModel.AppendOutputText("string[] result = regex.Split(inputData);"); return OperationIsComplete(worker, viewModel); }
public bool Replace(BackgroundWorker worker, RegexTesterPageViewModel viewModel, RegexProcessContext context) { viewModel.AppendOutputText("string pattern = @\"" + StringFormat(context.MatchRegexExpression) + "\";"); viewModel.AppendOutputText("RegexOptions regexOptions = " + GetRegexOptions(context.MatchRegexOptions) + ";"); viewModel.AppendOutputText("Regex regex = new Regex(pattern, regexOptions);"); viewModel.AppendOutputText("string inputData = @\"" + StringFormat(context.InputText) + "\";"); viewModel.AppendOutputText("string replacement = @\"" + StringFormat(context.ReplaceRegexPattern) + "\";"); viewModel.AppendOutputText("string result = regex.Replace(inputData, replacement);"); return OperationIsComplete(worker, viewModel); }
public bool Match(BackgroundWorker worker, RegexTesterPageViewModel viewModel, RegexProcessContext context) { viewModel.AppendOutputText("string pattern = @\"" + StringFormat(context.MatchRegex.ToString()) + "\";"); viewModel.AppendOutputText("RegexOptions regexOptions = " + GetRegexOptions(context.MatchRegex.Options) + ";"); viewModel.AppendOutputText("Regex regex = new Regex(pattern, regexOptions);"); viewModel.AppendOutputText("string inputData = @\"" + StringFormat(context.InputText) + "\";"); viewModel.AppendOutputText("foreach (Match match in regex.Matches(inputData))"); viewModel.AppendOutputText("{"); viewModel.AppendOutputText("\tif (match.Success)"); viewModel.AppendOutputText("\t{"); viewModel.AppendOutputText("\t\t//TODO processing results"); viewModel.AppendOutputText("\t}"); viewModel.AppendOutputText("}"); return OperationIsComplete(worker, viewModel); }
public bool Match(BackgroundWorker worker, RegexTesterPageViewModel viewModel, RegexProcessContext context) { if (!RegexIsMatch(context)) { return OperationIsComplete(worker); } var matches = context.MatchRegex.Matches(context.InputText); int count = 0; var isSimpleMatch = string.IsNullOrEmpty(context.ReplaceRegexPattern); Dictionary<String, String> groups = GetMatchGroups(context, isSimpleMatch); foreach (Match item in matches) { if (worker.CancellationPending) return false; if (item.Success) { if (isSimpleMatch) { viewModel.AppendOutputText(item.Value); } else { string result = context.ReplaceRegexPattern; foreach (var group in groups) { result = result.Replace(group.Value, item.Groups[group.Key].Value); } viewModel.AppendOutputText(result); } count++; } } viewModel.Count = count; return OperationIsComplete(worker); }
private void RunProcess() { if (!initialize) return; string matchPattern = GetInputText(rtbInputRegex); #region Check matchPattern viewModel.PrepareProcess(); if (string.IsNullOrEmpty(matchPattern)) { ClearRegexSyntaxError(rtbInputRegex, rtbInputReplace); viewModel.CompleteProcess(); ClearResultData(); return; } try { Regex checkInputRegex = new Regex(matchPattern); ClearRegexSyntaxError(rtbInputRegex, rtbInputReplace); } catch (Exception ex) { AddRegexSyntaxError(rtbInputRegex, ex.Message); viewModel.CompleteProcess(); ClearResultData(); return; } #endregion if (worker.IsBusy) { worker.CancelAsync(); int timeout = process_is_busy ? 500 : 100; System.Threading.Thread.Sleep(timeout); } RegexProcessContext context = new RegexProcessContext(); context.MatchRegex = new Regex(matchPattern, GetRegexOptions()); context.ReplaceRegexPattern = GetInputText(rtbInputReplace); context.CurrentMode = GetCurrentMode(); context.InputText = tbInputText.Text; context.OutputMode = GetOutputMode(); if (!worker.IsBusy) { worker.RunWorkerAsync(context); } else { if (process_is_busy && viewModel.Autorun) { process_is_busy = false; viewModel.Autorun = false; } else { process_is_busy = true; RunProcess(); } } }
public bool Split(BackgroundWorker worker, RegexTesterPageViewModel viewModel, RegexProcessContext context) { if (!RegexIsMatch(context)) { return OperationIsComplete(worker); } int count = 0; var matches = context.MatchRegex.Split(context.InputText); foreach (var str in matches) { if (worker.CancellationPending) return false; if (!string.IsNullOrEmpty(str)) { viewModel.AppendOutputText(str); count++; } } viewModel.Count = count; return OperationIsComplete(worker); }
private bool RegexIsMatch(RegexProcessContext context) { return context.MatchRegex.IsMatch(context.InputText); }
private static Dictionary<String, String> GetMatchGroups(RegexProcessContext context, bool isSimpleMatch) { Dictionary<String, String> groups = new Dictionary<String, String>(); if (isSimpleMatch) return groups; foreach (var groupName in context.MatchRegex.GetGroupNames()) { string group = string.Format("${{{0}}}", groupName); if (context.ReplaceRegexPattern.Contains(group)) { groups.Add(groupName, group); } } return groups; }