Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match numbers starting with 1, maximum 12 digits

Tags:

regex

I am searching for a regular expression that matches any string that is:

  1. A number which is greater than zero
  2. The number has at least one digit
  3. No more than 12 digits

I tried this one without success:

^[1-9][0-9]*{1,12}$
like image 672
NoobieNoob Avatar asked Dec 11 '22 14:12

NoobieNoob


2 Answers

If numbers with leading zeros that are greater than zero are allowed, you can use ^(?!0+$)[0-9]{1,12}$ if the tool/language you use supports lookaheads. The lookahead is used to ensure that the number doesn't entirely consist of zeros.

like image 64
Sebastian Proske Avatar answered Jan 10 '23 05:01

Sebastian Proske


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

Start with a single digit between 1-9, then have 0 to 11 occurrences of a digit between 0-9

like image 36
Vasan Avatar answered Jan 10 '23 06:01

Vasan