3

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.

4
  • is it valid 0123456789? Commented Jul 25, 2014 at 1:41
  • yes.. if other number than in the list is valid. Commented Jul 25, 2014 at 1:42
  • I think you want something as simple as ^(?!(\\d)\\1+$|1234567890$)\\d{10,}$. See demo Commented Jul 25, 2014 at 2:06
  • 1
    yes @HamZa i tested it and it works as i expected. thank you :D Commented Jul 25, 2014 at 3:18

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

FYI Added regex demo and explanation. :)
I think you need to add $ or \\b after 1234567890 :)
My assumption was that he doesn't want 1234567890 at all at the start of the string, e.g., 1234567890555 should fail, but if not, then you're right we need to add a boundary. :)
@HamZa You and I know that the real problem is if he gets back and says "I also don't want 2345678901, 9876543210 etc... Problematic for regex unless we want to build a big DENY list.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.