.NET正则表达式,只有数字,没有空格 [英] .NET Regex, only numeric, no spaces

查看:176
本文介绍了.NET正则表达式,只有数字,没有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找那个只接受数值,并没有空格定期EX pression。 我目前使用的:

I'm looking for a regular expression that accepts only numerical values and no spaces. I'm currently using:

^(0|[1-9][0-9]*)$

这工作得很好,但它接受值由空间而已。有什么不对呢?

which works fine, but it accepts values that consist ONLY of spaces. What is wrong with it?

推荐答案

之所以是 * 将接受0以上。纯粹的空字符串0的数字,因此满足要求。您需要1个或多个这样使用 + 代替。

The reason why is that * will accept 0 or more. A purely empty string has 0 numbers and hence meets the requirements. You need 1 or more so use + instead.

^(0|[1-9][0-9]+)$

修改

下面是安德鲁斯更强大,更简单的解决方案。

Here is Andrews more robust and simpler solution.

^\d+$

这篇关于.NET正则表达式,只有数字,没有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆