-1

I have a case that I only care about the maximum number. For instance, consider that I have to simply check that the string is: "max of 10 numeric digits", which means that it should meet the following:

  • It contains only numbers (resolved).
  • It has to be 10 digits at maximum.

I read about limiting the length, I came up with the following result:

  • ^\d{10}$: all numerics, 10 numbers specifically.
  • ^\d{10,20}$: all numerics, 10 - 20 length.
  • ^\d{10,}$: all numerics, 10 at minimum.

However, ^\d{,10}$ is invalid! Is there a specific way to do it, or should I do it as ^\d{1,10}$ ?

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • You can use; `^\d{0,10}$` as well – anubhava Jun 16 '19 at 14:17
  • @anubhava `^\d{0,10}$` means that if there is no characters at all, it would be vaild. Correct? – Ahmad F Jun 16 '19 at 14:21
  • Yes that's correct,. It will match an empty string as well. If you don't want that then `^\d{1,10}$` – anubhava Jun 16 '19 at 14:22
  • @AhmadF the first number in the curly brackets is the lowest number of occurences of the element before the brackets. If you want between 1 and 10, use `^\d{1,10}$` – Seblor Jun 16 '19 at 14:22
  • Some regex engines do actually support `^\d{,10}$`, such as python. Can you specify a language so that we can give more useful answers? Regex behaviour and syntax differ a lot from flavour to flavour. – Sweeper Jun 16 '19 at 15:39
  • Hi @Sweeper, good to see you here :) Thanks for the tip. However, I think that I have to go with `^\\d{1,10}$` – Ahmad F Jun 16 '19 at 15:41
  • @AhmadF In TRE, there [is a bug](https://stackoverflow.com/questions/46999964/unexpected-match-of-regex/47004617#47004617) with `^\d{,10}$`. What am I talking about, even `^.{273}$` [won't work in TRE](https://ideone.com/LMrCQu). – Wiktor Stribiżew Jun 16 '19 at 17:07

1 Answers1

0

^\d{1,10}$ or anubhava's advice in the comment might simply suffice, yet there might be other excessive expressions such as:

^(?=[0-9]).{1,10}$
^(?=[0-9])\d{1,10}$

that might work.

Demo

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69