Reputation: 41
I'm a newbie in java regex. i am seeking for advise for this series of number checking :
Number, must be >= 10 digits, user is not allowed to input as follows:
"0000000000","1111111111","2222222222","3333333333","4444444444",
"5555555555","6666666666","7777777777","8888888888","9999999999",
"1234567890","00000000000","11111111111","22222222222","33333333333",
"44444444444","55555555555","66666666666","77777777777","88888888888",
"99999999999"
currently my regex pattern is something like this
^(?=\\d{8,11}$)(?:(.)\\1*)$
this validates all numbers in the series except the 1234567890
. any advise is appreciated. Thank you.
Upvotes: 3
Views: 1618
Reputation: 46891
Number, must be >= 10 digits, user is not allowed to input as follows.
You can use String.matches()
method to check for any match.
Try below regex that checks for possible inputs as suggested by you. add more as per your need.
1234567890|(\d)\1{9}
Here is Live demo
Pattern explanation:
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)
Sample code:
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
Upvotes: 0
Reputation: 41848
Use this:
^(?!(\d)\1+\b|1234567890)\d{10,}$
See what matches and fails in the Regex Demo.
To validate in Java, with matches
we don't need the anchors:
if (subjectString.matches("(?!(\\d)\\1+\\b|1234567890)\\d{10,}")) {
// It matched!
}
else { // nah, it didn't match...
}
Explanation
(?!(\d)\1+\b|1234567890)
asserts that what follows is not...(\d)\1+\b
one digit (captured to Group 1), follows by repetitions of what was matched by Group 1, then a word boundary|
1234567890
\d{10,}
matches ten or more digitsUpvotes: 1