正则表达式适用于任何类型的数值 [英] Regex for any type of numeric value
问题描述
大家好。
问题:我有一个文本框,我想只允许数字值到这个文本框,如.2,2.3, 23.233
解决方案:对于上述情况,我已经尝试了正则表达式(^ \ * *(?=。* [1-9])\d *( ?:\。\\\ {1,4})?\ * * $)
但仍然不能像点值那样允许(例如.2)
所以,我需要知道他们有任何其他可用的解决方案.....请帮助我摆脱它
)
但仍然不能像点值那样允许(。2)
所以,我需要知道他们有任何其他可用的解决方案.....请帮助我摆脱它
试试这个\d +(\\ \\\\ +)?。你可以在网上找到很多例子。只是谷歌!
更好用
double val;
< span class =code-keyword> if ( double .TryParse(YourTextbox.Text, out val))
{
// 确定
}
else
{
// 不是数字
}
该策略还将应对不同的区域设置(小数点而不是小数点怎么办? ?)。
Hi,everyone
Question: i have one text-box and i want to allow only numeric value to this text-box like i.e .2,2.3,23.233
Solution: For the above, i already try regex ("^\s*(?=.*[1-9])\d*(?:\.\d{1,4})?\s*$")
but still it can`t be allow after point value like example(".2")
so, i need to know that their are any other solutions available .....please help me to get-out of it
")
but still it can`t be allow after point value like example(".2")
so, i need to know that their are any other solutions available .....please help me to get-out of it
Try this "\d+(\.\d+)?". You can find plenty of examples over the net. Just Google!
Better use
double val; if (double.TryParse(YourTextbox.Text, out val)) { //OK } else { //not numeric }
That strategy will also cope with different regional settings (what about a decimal comma instead of a decimal point?).
这篇关于正则表达式适用于任何类型的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!