我是java正则表达式的新手。我正在为这一系列的号码检查寻求建议:
数字,必须是> = 10位数,不允许用户输入如下:
"0000000000","1111111111","2222222222","3333333333","4444444444",
"5555555555","6666666666","7777777777","8888888888","9999999999",
"1234567890","00000000000","11111111111","22222222222","33333333333",
"44444444444","55555555555","66666666666","77777777777","88888888888",
"99999999999"
目前我的正则表达式是这样的
^(?=\\d{8,11}$)(?:(.)\\1*)$
这将验证除1234567890
以外的系列中的所有数字。任何建议表示赞赏。谢谢。
答案 0 :(得分:1)
使用此:
^(?!(\d)\1+\b|1234567890)\d{10,}$
在 the Regex Demo 中查看匹配和失败的内容。
要使用matches
在Java中进行验证,我们不需要锚点:
if (subjectString.matches("(?!(\\d)\\1+\\b|1234567890)\\d{10,}")) {
// It matched!
}
else { // nah, it didn't match...
}
<强>解释强>
(?!(\d)\1+\b|1234567890)
声称接下来的内容不是...... (\d)\1+\b
一个数字(捕获到第1组),然后重复第1组匹配的内容,然后是单词边界|
1234567890
\d{10,}
匹配十位或更多位数答案 1 :(得分:0)
数字,必须是&gt; = 10位数,不允许用户输入如下。
您可以使用String.matches()
方法检查是否有任何匹配。
尝试以下正则表达式,检查您建议的可能输入。根据您的需要添加更多。
1234567890|(\d)\1{9}
模式说明:
1234567890 '1234567890'
| OR
( group and capture to \1:
\d digits (0-9)
) end of \1
\1{9} what was matched by capture \1 (9 times)
示例代码:
String regex ="1234567890|(\\d)\\1{9}";
System.out.println("0000000000".matches(regex)); // true
System.out.println("1234567890".matches(regex)); // true
System.out.println("1111111111".matches(regex)); // true