简体   繁体   English

正则表达式匹配仅不以零开头的数字

[英]Regular expression to match numbers only not starting with zero

I have a textbox on the form and I want the user to be able to enter only numbers, and first number can not be zero. 我在表单上有一个文本框,我希望用户只能输入数字,并且第一个数字不能为零。 Which pattern must be in this case? 在这种情况下必须使用哪种模式?

Use this expression 使用这个表达

string expression = @"^[1-9]\d*$";

For anyone wanting to test the expression, use this link: http://www.rubular.com/r/1JIPP8E1zH 对于想要测试该表达式的任何人,请使用以下链接: http : //www.rubular.com/r/1JIPP8E1zH

Try with: 尝试:

/^[1-9][0-9]*$/

What length of string you accept ? 您接受多长的字符串?

If you allow empty string too try with: 如果您也允许使用空字符串,请尝试:

/^([1-9][0-9]*)?$/

Alternatively, and I would suggest this for localization purposes, consider 另外,我建议出于本地化目的考虑,

 double.Parse(myTextBox.Text, System.Globalization.CultureInfo.CurrentCulture);

as alternative. 作为替代。 It parses numbers with decimals or whatever variation you like according to the settings (culture) which is installed. 它根据安装的设置(区域性)使用十进制或任何您喜欢的变体形式解析数字。 In my country, with a dot as decimals seperator. 在我国,用小数点分隔符作为点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM