Regular expression to match integers up to 9 digits

I want to create a regular expression where only numbers with a maximum length of 9 and a minimum length are allowed. I came up with \d{9}[0-9] , but it does not work.

+6
source share
4 answers

You're close Try the following:

 ^\d{0,9}$ 

The values ​​^ and $ correspond to the beginning and end of the text, respectively. \d{0,9} matches anywhere on the line, so d0000 will go through because it will match 0000 , although it has d in it, which I don’t think you want. That is why they should be there.

+17
source

Regular expressions can be complex; what you wrote does the following:

  • \d - digit
  • \d{9} - exactly 9 digits
  • \d{9}[0-9] - exactly 9 digits followed by something between 0 and 9

If you do not need a minimum length limit, but a maximum length of 9, you will probably need the following regular expression:

  • \d{0,9} - 0 to 9 digits
+11
source

Looks like you were around, try \d{0,9} .

+2
source

I think it should be 1 to 9 digits:

 ^\d{1,9}$ 
+2
source

Source: https://habr.com/ru/post/889713/


All Articles