Exemplo n.º 1
0
 public static bool IsMixedKebabSnakeCase(this string check,
                                          int minLength     = 2,
                                          int maxLength     = 20,
                                          bool allowNumbers = true,
                                          RegexLetterCase allowedLetters = RegexLetterCase.LowerOnly)
 {
     return(CheckSymbolSeparatedCase(check, minLength, maxLength, allowNumbers, allowedLetters, '_', '-'));
 }
Exemplo n.º 2
0
 /// <summary>
 /// Checks if a string is in dot.notation.
 /// Allows to set length constraints and include/exclude numbers.
 /// </summary>
 /// <param name="check"></param>
 /// <param name="minLength"></param>
 /// <param name="maxLength"></param>
 /// <param name="allowNumbers"></param>
 /// <returns></returns>
 public static bool IsDotNotation(
     this string check,
     int minLength     = 2,
     int maxLength     = 20,
     bool allowNumbers = true,
     RegexLetterCase allowedLetters = RegexLetterCase.LowerOnly)
 {
     return(CheckSymbolSeparatedCase(check, minLength, maxLength, allowNumbers, allowedLetters, '.'));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Internal check for kebab and snake case.
        /// </summary>
        /// <param name="check"></param>
        /// <param name="kebab"></param>
        /// <param name="minLength"></param>
        /// <param name="maxLength"></param>
        /// <param name="allowNumbers"></param>
        /// <returns></returns>
        private static bool CheckSymbolSeparatedCase(
            string check,
            int minLength,
            int maxLength,
            bool allowNumbers,
            RegexLetterCase allowedLetters,
            params char[] symbols)
        {
            ValidateLengthInputs(minLength, maxLength);

            int len = check.Length;

            if (len < minLength || len > maxLength)
            {
                return(false);
            }

            string numPattern    = allowNumbers ? "0-9" : String.Empty;
            string letterPattern = String.Empty;

            // determine which letters are allowed
            switch (allowedLetters)
            {
            case RegexLetterCase.LowerOnly:
                letterPattern = "a-z";
                break;

            case RegexLetterCase.CapitalOnly:
                letterPattern = "A-Z";
                break;

            case RegexLetterCase.Both:
                letterPattern = "A-Za-z";
                break;
            }

            string pattern = String.Format("^[{2}{0}]+([{1}]{{1}}[{2}{0}]+)*$", numPattern, String.Join(String.Empty, symbols), letterPattern);

            return(check.IsRegexMatch(pattern));
        }