private static string AddMathematicalSignOption(this string numericPattern, MathematicalSignOption mathematicalSignOption, NumberFormatInfo numberFormat) { if (numberFormat.NumberNegativePattern == 0) { throw new NotImplementedException($"{nameof(numberFormat.NumberNegativePattern)} of value {numberFormat.NumberNegativePattern} has not yet been implemented."); } bool before = numberFormat.NumberNegativePattern == 1 || numberFormat.NumberNegativePattern == 2; switch (mathematicalSignOption) { case MathematicalSignOption.None: return(numericPattern); case MathematicalSignOption.Positive when before: return(numericPattern.Replace("^", $"^[{numberFormat.PositiveSign}]")); case MathematicalSignOption.Positive: return(numericPattern.Replace("$", $"[{numberFormat.PositiveSign}]$")); case MathematicalSignOption.Negative when before: return(numericPattern.Replace("^", $"^[{numberFormat.NegativeSign}]")); case MathematicalSignOption.Negative: return(numericPattern.Replace("$", $"[{numberFormat.NegativeSign}]$")); case MathematicalSignOption.PositiveOrNone when before: return(numericPattern.Replace("^", $"^[{numberFormat.PositiveSign}]?")); case MathematicalSignOption.PositiveOrNone: return(numericPattern.Replace("$", $"[{numberFormat.PositiveSign}]?$")); case MathematicalSignOption.NegativeOrNone when before: return(numericPattern.Replace("^", $"^[{numberFormat.NegativeSign}]?")); case MathematicalSignOption.NegativeOrNone: return(numericPattern.Replace("$", $"[{numberFormat.NegativeSign}]?$")); case MathematicalSignOption.PositiveOrNegative when before: return(numericPattern.Replace("^", $"^[{numberFormat.PositiveSign}{numberFormat.NegativeSign}]")); case MathematicalSignOption.PositiveOrNegative: return(numericPattern.Replace("$", $"[{numberFormat.PositiveSign}{numberFormat.NegativeSign}]$")); case MathematicalSignOption.Any when before: return(numericPattern.Replace("^", $"^[{numberFormat.PositiveSign}{numberFormat.NegativeSign}]?")); case MathematicalSignOption.Any: return(numericPattern.Replace("$", $"[{numberFormat.PositiveSign}{numberFormat.NegativeSign}]?$")); default: throw new ArgumentOutOfRangeException(nameof(mathematicalSignOption), mathematicalSignOption, null); } }
/// <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))); }