正则表达式只接受数字 [英] Regex to accept only numbers
本文介绍了正则表达式只接受数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要只接受数字0-9而没有任何特殊字符或小数的常规exp。也不允许单个0但是0后跟其他数字是允许的。
I want the regular exp which accept only numbers 0-9 without any special character or decimal.Also single 0 should not be allowed but 0 followed with other numbers are allow.
推荐答案
尝试:
Try:
[1-9]\d*|0\d+
添加第二部分到正则表达式 - OriginalGriff [/ edit]
[edit]Added second part to regex - OriginalGriff[/edit]
解决方案
正则表达式 -(^ [0] [1-9] +)|([1-9] \d *)
在线测试 - 此处 [< a href =http://www.rubular.com/r/MmgRAJZjGotarget =_ blanktitle =New Window> ^ ]。
示例
12345678 - 12345678
0 - 未配对。
01567 - 01567
0000 - 不匹配。
bbbb - 不匹配。
1 9999 - 空格字符不匹配,而是将其视为两个不同的数字,如1, 9999。
注意
- >所有测试字符串都写在左框中。
- >所有匹配的结果都写在右边框中。
- >您还可以检查为匹配的字符串执行的组(在右侧匹配结果框下方的匹配组框中),
其中 Group-1 是(^ [0] [1-9] +)
和 Group-2 是([1-9] \\ \\ d *)
。
例如 - 12345678由Group-2匹配,而不是Group- 1.
谢谢......
Solution
Regular expression -(^[0][1-9]+)|([1-9]\d*)
Test online - Here[^].
Examples
12345678 - 12345678
0 - Not mached.
01567 - 01567
0000 - Not matched.
bbbb - Not matched.
1 9999 - Space character is not matched, instead it is taken as two different numbers as "1", "9999".
Note
-> All the test strings are written in left box.
-> All the matched results are written in right box.
-> You can also check, which group got executed for the matched string (in box "Match groups" below the right "Match result" box),
where Group-1 is(^[0][1-9]+)
and Group-2 is([1-9]\d*)
.
For example - 12345678 is matched by Group-2 and not by Group-1.
Thanks...
这篇关于正则表达式只接受数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文