/// <inheritdoc />
        public string CreateQuantifier(IRegularExpressionFactoryContext ctx, Quantifier quantity, string quantifiedContentRegex)
        {
            if (string.IsNullOrEmpty(quantifiedContentRegex))
            {
                return(string.Empty);
            }

            quantifiedContentRegex = CreateNonCapturingGroup(ctx, quantifiedContentRegex);

            var defined = quantity.ToDefinedRange();

            var min = defined.Min.MaxUInt;
            var max = defined.Max.MaxUInt;

            if (min == 1 && max == 1)
            {
                return(quantifiedContentRegex);
            }

            return(CreateNonCapturingGroup(ctx, GetQuantifiedContent()));


            string GetQuantifiedContent()
            {
                if (min == 0 && max == 1)
                {
                    return($"{quantifiedContentRegex}?");
                }

                if (defined.Max.IsInfinite)
                {
                    if (min == 0)
                    {
                        return($"{quantifiedContentRegex}*");
                    }

                    if (min == 1)
                    {
                        return($"{quantifiedContentRegex}+");
                    }

                    return($"{quantifiedContentRegex}{{{min},}}");
                }

                if (min == max)
                {
                    return($"{quantifiedContentRegex}{{{min}}}");
                }

                return($"{quantifiedContentRegex}{{{min},{max}}}");
            }
        }
        /// <inheritdoc />
        public string CreateCharRange(IRegularExpressionFactoryContext ctx, char firstCharacter, char lastCharacter)
        {
            if (firstCharacter > lastCharacter)
            {
                var tmp = firstCharacter;
                firstCharacter = lastCharacter;
                lastCharacter  = tmp;
            }

            if (firstCharacter == lastCharacter)
            {
                return(CharToRegex(firstCharacter));
            }

            return($"[{CharToRegex(firstCharacter)}-{CharToRegex(lastCharacter)}]");
        }
Ejemplo n.º 3
0
 public RegularExpressionBuildingContext(IRegularExpressionFactory factory, IRegularExpressionFactoryContext context)
 {
     Factory = factory;
     Context = context;
 }
        /// <inheritdoc />
        public string CreateNamedGroup(IRegularExpressionFactoryContext ctx, string groupName, string regexContent, out string factualGroupName)
        {
            factualGroupName = ctx.GetUniqueAndValidGroupNameFor(groupName ?? string.Empty);

            return($"(?<{factualGroupName}>{regexContent})");
        }
 /// <inheritdoc />
 public string CreateAlternation(IRegularExpressionFactoryContext ctx, IEnumerable <string> alternativeRegexs)
 {
     return(CreateNonCapturingGroup(ctx, string.Join("|", alternativeRegexs)));
 }
 /// <inheritdoc />
 public string CreateNot(IRegularExpressionFactoryContext ctx, string negatedContentRegex)
 {
     return($"(?:(?:(?!{negatedContentRegex}).*)|.+(?:{negatedContentRegex}).*|.*(?:{negatedContentRegex}).+)");
 }
 /// <inheritdoc />
 public string GetDigitCharacter(IRegularExpressionFactoryContext ctx) => @"[0-9]";
 /// <inheritdoc />
 public string GetWordCharacter(IRegularExpressionFactoryContext ctx) => @"\w";
 /// <inheritdoc />
 public string CreateCharSet(IRegularExpressionFactoryContext ctx, IEnumerable <char> characters)
 {
     return($"[{this.CharsToRegex(characters)}]");
 }
 /// <inheritdoc />
 public string CreateCharRange(IRegularExpressionFactoryContext ctx, StandardCharacterCategories range)
 {
     return(range.ToRegexStatement());
 }
 /// <inheritdoc />
 public string CreateCapturingGroup(IRegularExpressionFactoryContext ctx, string groupContentRegex)
 {
     return($"({groupContentRegex})");
 }
 /// <inheritdoc />
 public string CreateRecursiveGroupCall(IRegularExpressionFactoryContext ctx, string regexGroupName)
 {
     return($"(?&{regexGroupName})");
 }
 /// <inheritdoc />
 public string CreateNegativeLookaheadGroup(IRegularExpressionFactoryContext ctx, string groupContentRegex)
 {
     return($"(?!{groupContentRegex})");
 }