在C#中使用RegExvalidation浮点数
我想在WPF中创建一个只有Numeric的TextBox
,我有这个代码:
void NumericTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = !IsValidInput(e.Text); } private bool IsValidInput(string p) { switch (this.Type) { case NumericTextBoxType.Float: return Regex.Match(p, "^[0-9]*[.][0-9]*$").Success; case NumericTextBoxType.Integer: default: return Regex.Match(p, "^[0-9]*$").Success; } } // And also this! public enum NumericTextBoxType { Integer = 0, Float = 1 }
当我将类型设置为Integer时,它运行良好,但对于Float,它没有。
我可以在那里使用这么多NumericTextBox
控件,但我想知道为什么这个不起作用?
试试这个:
@"^[0-9]*(?:\.[0-9]*)?$"
你需要逃避这段时期。 并且使句点和小数部分可选是一个好主意。
如果你需要处理负值,你可以添加-?
在每个模式中的第一个[0-9]
之前。
更新
测试如下:
var regex = new Regex(@"^[0-9]*(?:\.[0-9]*)?$"); Console.WriteLine(new bool[] {regex.IsMatch("blah"), regex.IsMatch("12"), regex.IsMatch(".3"), regex.IsMatch("12.3"), regex.IsMatch("12.3.4")});
结果是
False True True True False
我劝你使用Double.TryParse()方法而不是正则表达式validation。 使用TryParse()
让您的应用程序在文化方面更具普遍性。 当前文化发生变化时, TryParse()
将解析没有问题。 TryParse()
方法也被认为没有错误,因为它们是由.net社区测试的:)。
但是在正则表达式的情况下,你应该改变你的validation表达,因此它可能与新文化无关。
你可以像这样重写代码:
private bool IsValidInput(string p) { switch (this.Type) { case NumericTextBoxType.Float: double doubleResult; return double.TryParse(p, out doubleResult); case NumericTextBoxType.Integer: default: int intResult; return int.TryParse(p, out intResult); } }
您甚至可以添加自己的扩展方法,使解析部分更加优雅。
public static double? TryParseInt(this string source) { double result; return double.TryParse(source, out result) ? result : (double?)null; } // usage bool ok = source.TryParseInt().HasValue;
查看您将在double,float和int上找到的TryParse
静态方法。
如果可以解析字符串,则返回true(通过Parse
方法)。
[-+]?\d+(.\d+)?
浮点数最简单的正则表达式。 它与案件’123’不符。 或’.123’。
此外,您应该查看上下文文化:
CultureInfo ci = CultureInfo.CurrentCulture; var decimalSeparator = ci.NumberFormat.NumberDecimalSeparator; var floatRegex = string.Format(@"[-+]?\d+({0}\d+)?", decimalSeparator);
我尝试了上面批准的解决方案,发现如果用户只输入一个点@"^[0-9]*(?:\.[0-9]*)?$"
,它将会失败。
所以,我把它修改为:
@"^[0-9]*(?:\.[0-9]+)?$"
这是我通过混合来自@Andrew Cooper和@Ramesh的回复而得出的代码。 添加了字典代码,因此任何考虑测试代码的人都可以轻松地运行尽可能多的测试用例。
//greater than or equal to zero floating point numbers Regex floating = new Regex(@"^[0-9]*(?:\.[0-9]+)?$"); Dictionary test_cases = new Dictionary(); test_cases.Add("a", floating.IsMatch("a")); test_cases.Add("a.3", floating.IsMatch("a.3")); test_cases.Add("0", floating.IsMatch("0")); test_cases.Add("-0", floating.IsMatch("-0")); test_cases.Add("-1", floating.IsMatch("-1")); test_cases.Add("0.1", floating.IsMatch("0.1")); test_cases.Add("0.ab", floating.IsMatch("0.ab")); test_cases.Add("12", floating.IsMatch("12")); test_cases.Add(".3", floating.IsMatch(".3")); test_cases.Add("12.3", floating.IsMatch("12.3")); test_cases.Add("12.3.4", floating.IsMatch("12.3.4")); test_cases.Add(".", floating.IsMatch(".")); test_cases.Add("0.3", floating.IsMatch("0.3")); test_cases.Add("12.31252563", floating.IsMatch("12.31252563")); test_cases.Add("-12.31252563", floating.IsMatch("-12.31252563")); foreach (KeyValuePair pair in test_cases) { Console.WriteLine(pair.Key.ToString() + " - " + pair.Value); }