1

I am new to regular expression. I need to validate the following using regular expression:

  • an input string is digit only between 6 and 10 characters long
2
  • Questions asking for code must demonstrate a minimal effort in solving the actual problem, including the attempted code and the encountered issues Commented Jan 21, 2014 at 22:29
  • Combining your regex with a java number class such as BigDecimal will assist you to remove leading and trailing unnecessary zero's of the number. Commented Mar 30, 2016 at 11:52

2 Answers 2

7

You can use the following RegEx, \\d{6,10}. This would match any string which has only digits and the number of times digits can occur is 6 to 10.

(By digit we mean any character with the Unicode General Category of Nd (Number, Decimal Digit.) as Java uses the ICU Regular Expressions libraries.)

You can see how the RegEx works here

    String pattern = "\\d{6,10}", myString = "111111";
    System.out.println(myString.matches(pattern));

would print

true
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you not only for the answer, but also for the regex tester site. This is exactly what I needed!
You really don't need the anchors, since you are using String#matches(); it acts as if the anchors are already there (if you don't believe me, try it out).
It's not correct that this regex matches "any string which starts and ends with a number". This matches
@Behe As long as the length is within the range 6-10 and it has only numbers, it matches OP's requirement :)
@thefourtheye: My comment is about the formulation, not the regex itself: "Only numbers" is different than "any string which starts and ends with a number".
|
2

you can use this code

[0-9]{6, 10}

or

\d{6, 10}

enjoy !

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.