示例#1
0
 /// <summary>
 /// Builds a numeric regular expression pattern.
 /// </summary>
 /// <param name="digitCount">Number of digits to the left of the decimal point to accept. A value of zero accepts any number of digits.</param>
 /// <param name="mathematicalSignOption">Determines how mathematical signs are handled.</param>
 /// <param name="numberGroupSeparatorOption">Determines how number group separators are handled.</param>
 /// <param name="decimalDigits">Number of digits to the right of the decimal point to accept.</param>
 /// <param name="decimalDigitRequired">Determines if the <see cref="decimalDigits"/> is required or optional.</param>
 /// <param name="whiteSpaceOption">Determines how white space is handled.</param>
 /// <param name="culture">The culture for which the regular expression is intended to be used. Defaults to the current culture.</param>
 public static string BuildPattern(
     int digitCount = 0,
     MathematicalSignOption mathematicalSignOption         = MathematicalSignOption.None,
     NumberGroupSeparatorOption numberGroupSeparatorOption = NumberGroupSeparatorOption.None,
     int decimalDigits                 = 0,
     bool decimalDigitRequired         = false,
     WhiteSpaceOption whiteSpaceOption = WhiteSpaceOption.None,
     CultureInfo culture               = null)
 {
     culture = culture ?? CultureInfo.CurrentCulture;
     return(Pattern
            .AddDigitCount(digitCount)
            .AddMathematicalSignOption(mathematicalSignOption, culture.NumberFormat)
            .AddNumberGroupSeparatorOption(numberGroupSeparatorOption, culture.NumberFormat)
            .AddDecimalDigits(decimalDigits, decimalDigitRequired, culture.NumberFormat)
            .AddWhiteSpaceOption(whiteSpaceOption));
 }
示例#2
0
 /// <summary>
 /// Builds a <see cref="System.Text.RegularExpressions.Regex"/> object with a numeric pattern.
 /// </summary>
 /// <param name="digitCount">Number of digits to the left of the decimal point to accept. A value of zero accepts any number of digits.</param>
 /// <param name="mathematicalSignOption">Determines how mathematical signs are handled.</param>
 /// <param name="numberGroupSeparatorOption">Determines how number group separators are handled.</param>
 /// <param name="decimalDigits">Number of digits to the right of the decimal point to accept.</param>
 /// <param name="decimalDigitRequired">Determines if the <see cref="decimalDigits"/> is required or optional.</param>
 /// <param name="whiteSpaceOption">Determines how white space is handled.</param>
 /// <param name="culture">The culture for which the regular expression is intended to be used. Defaults to the current culture.</param>
 public static Regex BuildRegex(
     int digitCount = 0,
     MathematicalSignOption mathematicalSignOption         = MathematicalSignOption.None,
     NumberGroupSeparatorOption numberGroupSeparatorOption = NumberGroupSeparatorOption.None,
     int decimalDigits                 = 0,
     bool decimalDigitRequired         = false,
     WhiteSpaceOption whiteSpaceOption = WhiteSpaceOption.None,
     CultureInfo culture               = null)
 {
     return(new Regex(BuildPattern(
                          digitCount,
                          mathematicalSignOption,
                          numberGroupSeparatorOption,
                          decimalDigits,
                          decimalDigitRequired,
                          whiteSpaceOption,
                          culture ?? CultureInfo.CurrentCulture)));
 }
示例#3
0
        private static string AddWhiteSpaceOption(this string pattern, WhiteSpaceOption whiteSpaceOption)
        {
            switch (whiteSpaceOption)
            {
            case WhiteSpaceOption.None:
                return(pattern);

            case WhiteSpaceOption.Leading:
                return(pattern.Replace("^", @"^\s*"));

            case WhiteSpaceOption.Trailing:
                return(pattern.Replace("$", @"\s*$"));

            case WhiteSpaceOption.LeadingOrTrailing:
                return(pattern.Replace("^", @"^\s*").Replace("$", @"\s*$"));

            default:
                throw new ArgumentOutOfRangeException(nameof(whiteSpaceOption), whiteSpaceOption, null);
            }
        }