Regex for validation of 9 numbers with single space
84 观看
2回复
6 作者的声誉
I need a regex for validating a 9digit number with a single space in-between
So far I have tried
^([0-9]{0-9})\s{1}$
作者: Sivanesh selvaraj
的来源
发布者: 2017 年 12 月 27 日
回应 2
3像
14596 作者的声誉
Code
^(?=\d+ \d+$)[\d ]{10}$
Results
Input
1 23456789
12 3456789
123 456789
1234 56789
12345 6789
123456 789
1234567 89
12345678 9
123456789
123456789
123456789
1 2 3456789
1 234567890
Output
Only matches are below.
1 23456789
12 3456789
123 456789
1234 56789
12345 6789
123456 789
1234567 89
12345678 9
Explanation
^
Assert position at the start of the line(?=\d+ \d+$)
Positive lookahead ensuring what follows matches the following\d+
Match one or more digitsMatch a literal space
\d+
Match one or more digits$
Assert position at the end of the line
[\d ]{10}
Match any digit or space character exactly 10 times$
Assert position at the end of the line
1像
96253 作者的声誉
Don't use regex.
- Check the length is 10.
Count the numbers and spaces:
int nSpaces = 0, nNumbers = 0; for (int i = 0; i < str.length(); ++i) { char c = str.charAt(i); if (c == ' ') nSpaces++; else if (c >= '0' && c <= '9') nNumbers++; }
Check that there are 9 numbers and 1 space.
if (nSpaces == 1 && nNumbers == 9) { ... }
来自类别的问题 :
- java Java和C#中的int和Integer有什么区别?
- java 如何在Java中确定路由器/网关的IP?
- java 根据XSD文件验证XML文件的最佳方法是什么?
- java 如何整理整数除法的结果?
- java 将List <Integer>转换为List <String>
- java 为什么我不能在接口中声明静态方法?
- java 关闭电脑
- java 如何用Java播放声音?
- java 何时选择已检查和未检查的例外
- java 在Java中覆盖equals和hashCode时应该考虑哪些问题?
- regex Learning Regular Expressions
- regex 我的正则表达式匹配太多。我如何使其停止?
- regex 如何在保留原始字符串的同时对字符串执行Perl替换?
- regex 用Java替换正则表达式匹配的第n个实例
- regex 如何用链接替换普通URL?
- regex 带有标志的Python re.sub不会替换所有出现的内容
- regex 如何在JavaScript中验证电子邮件地址
- regex 如何在Java中转义正则表达式的文本
- regex 正则表达式将匹配Java方法声明
- regex 在Python中用空格分割字符串 - 保留引用的子字符串