75

I need a regular expression that requires at least ONE digits and SIX maximum.

I've worked out this, but neither of them seems to work.

^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$

^[0-999999]$

Any other suggestion?

Andrew
  • 946
  • 9
  • 19
framara
  • 2,833
  • 5
  • 28
  • 32

7 Answers7

182

You can use range quantifier {min,max} to specify minimum of 1 digit and maximum of 6 digits as:

^[0-9]{1,6}$

Explanation:

^     : Start anchor
[0-9] : Character class to match one of the 10 digits
{1,6} : Range quantifier. Minimum 1 repetition and maximum 6.
$     : End anchor

Why did your regex not work ?

You were almost close on the regex:

^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$

Since you had escaped the ? by preceding it with the \, the ? was no more acting as a regex meta-character ( for 0 or 1 repetitions) but was being treated literally.

To fix it just remove the \ and you are there.

See it on rubular.

The quantifier based regex is shorter, more readable and can easily be extended to any number of digits.

Your second regex:

^[0-999999]$

is equivalent to:

^[0-9]$

which matches strings with exactly one digit. They are equivalent because a character class [aaaab] is same as [ab].

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 2
    Just as the title suggests, a note could be added that to match a 6 digit number, one should use `[0-9]{6}`, and add anchors if need be. – Wiktor Stribiżew Jul 26 '16 at 12:50
30
  ^\d{1,6}$

....................

LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 1
    short and sweet. I used a variation of this \d{7,7} with Notepad++'s TextFX Find/Replace feature to get all 7 digit numbers in a file. many thanks for leading me to the solution I needed. – Chris Smith Sep 13 '13 at 16:29
  • 5
    Since `\d` and `[0-9]` are not always the same [(see this post)](http://stackoverflow.com/q/890686/1447415), the `^[0-9]{1,6}$` solution might be preferable. – Johan Nov 06 '13 at 09:34
  • Simple and effective. – Kenan Jul 24 '17 at 15:16
14

You could try

^[0-9]{1,6}$

it should work.

James
  • 65,548
  • 14
  • 155
  • 193
4

^[0-9]{1,6}$ should do it. I don't know VB.NET good enough to know if it's the same there.

For examples, have a look at the Wikipedia.

eckes
  • 64,417
  • 29
  • 168
  • 201
2
\b\d{1,6}\b

Explanation

\b    # word boundary - start
\d    # any digits between 0 to 9 (inclusive)
{1,6} # length - min 1 digit or max 6 digits
\b    # word boundary - end
SridharKritha
  • 8,481
  • 2
  • 52
  • 43
0

^[a-zA-Z0-9]{1,6}$

regex 6 digit number and alphabet in angular

bpfrd
  • 945
  • 3
  • 11
-4
/^[0-9][0-9][0-9][0-9]$/

Enter 4 digit number only

Sushil
  • 670
  • 7
  • 14
  • The question specifically asked for validation of a number between 1 and 6 digits. Can you explain how your answer helps solve that problem? Note that there are already several answers here that are now 10 years old, please make sure that if you post a new answer that you actually add something that is lacking in the existing posts. – Martijn Pieters Jun 09 '21 at 10:55