public async Task <RegExModel> RegEx(RegExModel request)
        {
            await Task.Run(() =>
            {
                var regex = new Regex(request.RegEx);

                if (regex.IsMatch(request.InputString))
                {
                    var match     = regex.Match(request.InputString);
                    request.Match = match.Value;

                    if (!string.IsNullOrEmpty(request.Replace))
                    {
                        request.Result = regex.Replace(request.InputString, request.Replace);
                    }
                    else
                    {
                        request.Result = "";
                    }
                }
                else
                {
                    request.Match  = "";
                    request.Result = "";
                }
            });

            return(request);
        }
        public string CreateRegularExpression(RegExModel model)
        {
            regexp  = "";
            regText = "";

            if (model.alphabet.isAlphabet)
            {
                if (model.alphabet.atLeastOne)
                {
                    regexp  = regexp + CreateFromAlphabet.FromAtLeastOne(model.alphabet);
                    regText = regText + CreateFromAlphabet.FromAtLeastOne(model.alphabet, model.alphabet.atLeastOne);
                }
                else
                {
                    regText = regText + CreateFromAlphabet.FromAlphabet(model.alphabet);
                }
            }

            if (model.number.isNumber)
            {
                if (model.number.atLeastOne)
                {
                    regexp  = regexp + CreateFromNumbers.FromAtLeastOne(model.number);
                    regText = regText + CreateFromNumbers.FromAtLeastOne(model.number, model.number.atLeastOne);
                }
                else
                {
                    regText = regText + CreateFromNumbers.FromNumber(model.number);
                }
            }

            if (model.special.isSpecial)
            {
                if (model.special.atLeastOne)
                {
                    regexp  = regexp + CreateFromSpecialCharacter.FromAtLeast(model.special);
                    regText = regText + CreateFromSpecialCharacter.FromAtLeast(model.special, model.special.atLeastOne);
                }
                else
                {
                    regText = regText + CreateFromSpecialCharacter.FromSpecialCharacter(model.special);
                }
            }

            TotalRegularExpression = regexp + "[" + regText + "]";

            if (model.totalLength.min > 0 || model.totalLength.max > 0)
            {
                if (model.totalLength.min == model.totalLength.max)
                {
                    TotalRegularExpression = TotalRegularExpression + "{" + model.totalLength.max + "}";
                }
                else if (model.totalLength.min > 0 && model.totalLength.max > 0)
                {
                    TotalRegularExpression = TotalRegularExpression + "{" + model.totalLength.min + "," + model.totalLength.max + "}";
                }
                else if (model.totalLength.min > 0)
                {
                    TotalRegularExpression = TotalRegularExpression + "{" + model.totalLength.min + ",}";
                }
            }

            if (model.separation.isSeparation)
            {
                TotalRegularExpression = AddSeparation(TotalRegularExpression, model.separation.separationText);
            }
            else
            {
                if (model.totalLength.min > 0 || model.totalLength.max > 0)
                {
                    TotalRegularExpression = TotalRegularExpression + "";
                }
                else
                {
                    TotalRegularExpression = TotalRegularExpression + "+";
                }
            }


            return(TotalRegularExpression);
        }
        public async Task <IActionResult> RegEx()
        {
            var model = new RegExModel();

            return(View(model));
        }
예제 #4
0
        public ActionResult RegEx(RegExModel model, string type)
        {
            try
            {
                var optionList = new RegexOptions();

                if (model.IgnoreCase)
                {
                    optionList |= RegexOptions.IgnoreCase;
                }

                if (model.Singleline)
                {
                    optionList |= RegexOptions.Singleline;
                }

                if (model.Multiline)
                {
                    optionList |= RegexOptions.Multiline;
                }

                if (model.CultureInvariant)
                {
                    optionList |= RegexOptions.CultureInvariant;
                }

                if (model.ECMAScript)
                {
                    optionList |= RegexOptions.ECMAScript;
                }

                if (model.ExplicitCapture)
                {
                    optionList |= RegexOptions.ExplicitCapture;
                }

                if (model.IgnorePatternWhitespace)
                {
                    optionList |= RegexOptions.IgnorePatternWhitespace;
                }

                if (model.RightToLeft)
                {
                    optionList |= RegexOptions.RightToLeft;
                }

                switch (type)
                {
                    case "replace-download":
                    case "replace":
                        string replacement = model.Replacement ?? string.Empty;
                        ViewBag.Replace = Regex.Replace(model.Input, model.Pattern, replacement, optionList);

                        if (type == "replace-download")
                        {
                            return this.File(Encoding.UTF8.GetBytes(ViewBag.Replace), "text/plain", "regex-replace.txt");
                        }

                        break;
                    case "matches":
                        var matches = Regex.Matches(model.Input, model.Pattern, optionList);
                        ViewBag.Matches = string.Format("{0} matches: {1}", matches.Count, string.Join(", ", matches.Cast<Match>().Select(x => x.Value).ToArray()));
                        break;
                    case "groups":
                        var match = Regex.Match(model.Input, model.Pattern, optionList);
                        if (match.Success)
                        {
                            ViewBag.Match = match;
                        }
                        break;
                }
            }
            catch (ArgumentException)
            {
                ModelState.AddModelError(string.Empty, "Your regular expression is invalid.");
            }

            return this.View();
        }