public static void StartMode(BackgroundWorker worker, RegexTesterPageViewModel viewModel, DoWorkEventArgs args) { viewModel.PrepareProcess(); RegexProcessContext context = (RegexProcessContext)args.Argument; bool complete = true; IRegexProcessStrategy strategy = RegexProcessStrategy[context.OutputMode]; if (context.CurrentMode == RegexMode.Match) { complete = strategy.Match(worker, viewModel, context); } else if (context.CurrentMode == RegexMode.Replace) { complete = strategy.Replace(worker, viewModel, context); } else { complete = strategy.Split(worker, viewModel, context); } if (!complete) { args.Cancel = true; } }
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)); }
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.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[] result = regex.Split(inputData);"); return(OperationIsComplete(worker, viewModel)); }
public bool Replace(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 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.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("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 bool OperationIsComplete(BackgroundWorker worker, RegexTesterPageViewModel viewModel) { viewModel.Count = 0; return(!worker.CancellationPending); }