/// <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)); }
/// <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))); }
private static string AddNumberGroupSeparatorOption(this string numericPattern, NumberGroupSeparatorOption numberGroupSeparatorOption, NumberFormatInfo numberFormat) { if (numberFormat.NumberGroupSizes.Length > 1) { throw new NotImplementedException("Support for multiple number group sizes has not yet been implemented."); } string numberGroupSize = numberFormat.NumberGroupSizes[0].ToString(); string numberGroupSeparator = Regex.Escape(numberFormat.NumberGroupSeparator); switch (numberGroupSeparatorOption) { case NumberGroupSeparatorOption.None: return(numericPattern); case NumberGroupSeparatorOption.Required: return(Regex.Replace(numericPattern.Replace(@"\d", $@"[\d]{{1,{numberGroupSize}}}(?:{numberGroupSeparator}[\d]{{{numberGroupSize}}})"), @"(?<!\[)\+(?!\])", "*")); case NumberGroupSeparatorOption.Optional: return(Regex.Replace(numericPattern.Replace(@"\d", $@"[\d]{{1,{numberGroupSize}}}(?:{numberGroupSeparator}?[\d]{{{numberGroupSize}}})"), @"(?<!\[)\+(?!\])", "*")); default: throw new ArgumentOutOfRangeException(nameof(numberGroupSeparatorOption), numberGroupSeparatorOption, null); } }