Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex validation for list of numbers

Tags:

java

regex

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.

like image 616
kenzouji Avatar asked Feb 02 '26 14:02

kenzouji


1 Answers

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

  • The negative lookahead (?!(\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
  • OR |
  • 1234567890
  • \d{10,} matches ten or more digits
like image 162
zx81 Avatar answered Feb 04 '26 06:02

zx81