29

I need a regular expression to match any number from 0 to 99. Leading zeros may not be included, this means that f.ex. 05 is not allowed.

I know how to match 1-99, but do not get the 0 included.

My regular expression for 1-99 is

^[1-9][0-9]?$
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
  • 1
    This was almost right, but you just had your optional argument '?' on the second digit instead of the leading digit. – Scott W Nov 21 '13 at 16:35

12 Answers12

46

There are plenty of ways to do it but here is an alternative to allow any number length without leading zeros

0-99:

^(0|[1-9][0-9]{0,1})$

0-999 (just increase {0,2}):

^(0|[1-9][0-9]{0,2})$

1-99:

^([1-9][0-9]{0,1})$

1-100:

^([1-9][0-9]{0,1}|100)$

Any number in the world

^(0|[1-9][0-9]*)$

12 to 999

^(1[2-9]|[2-9][0-9]{1}|[1-9][0-9]{2})$
Guish
  • 4,968
  • 1
  • 37
  • 39
19

Updated:

^([0-9]|[1-9][0-9])$

Matches 0-99. Doesn't match values with leading zeros. Depending on your application you may need to escape the parentheses and the or symbol.

Manny D
  • 20,310
  • 2
  • 29
  • 31
4
^(0|[1-9][0-9]?)$

Test here http://regexr.com?2uu31 (various samples included)

You have to add a 0|, but be aware that the "or" (|) in Regexes has the lowest precedence. ^0|[1-9][0-9]?$ in reality means (^0)|([1-9][0-9]?$) (we will ignore that now there are two capturing groups). So it means "the string begins with 0" OR "the string ends with [1-9][0-9]?". An alternative to using brackets is to repeat the ^$, like ^0$|^[1-9][0-9]?$.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • I tried everything but not this.. :( Why do I need the outer brackets?? I don´t understand. – AGuyCalledGerald Oct 13 '11 at 12:46
  • @Jan-FrederikCarl Because the `|` has precedence nothing. So without the brackets, `^0|[1-9][0-9]?$ == (?:^0)|(?:[1-9][0-9]?$)` (where `(?: )` is the non-capturing group). But what you want is something equivalent to `(?:^0$)|(?:^[1-9][0-9]?$)` – xanatos Oct 13 '11 at 12:48
2

A simpler answer without using the or operator makes the leading digit optional:

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

Matches 0-99 disallowing leading zeros (01-09).

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
Scott W
  • 202
  • 2
  • 12
2

console.log(/^0(?! \d+$)/.test('0123')); // true
console.log(/^0(?! \d+$)/.test('10123')); // false
console.log(/^0(?! \d+$)/.test('00123')); // true
console.log(/^0(?! \d+$)/.test('088770123')); // true

How about this?

Gaurav
  • 821
  • 9
  • 11
2

[...] but do not get the 0 included.

Just add 0|... in front of the expression:

^(0|[1-9][0-9]?)$
  ^^
aioobe
  • 413,195
  • 112
  • 811
  • 826
1

Answer:

^([1-9])?(\d)$

Explanation:

^          // beginning of the string
([1-9])?   // first group (optional) in range 1-9 (not zero here)
(\d)       // second group matches any digit including 0
$          // end of the string

Same as (Not grouping):

^[1-9]?\d$

Test:

https://regex101.com/r/Tpe9Ia/1

Ro.
  • 1,525
  • 1
  • 14
  • 17
1

This should do the trick:

^(?:0|[1-9][0-9]?)$
Oldskool
  • 34,211
  • 7
  • 53
  • 66
0

Try this it will help you

^([0-9]|[1-9][0-9])$
Shoaib Quraishi
  • 232
  • 2
  • 17
0
([1-9][0-9]+).*

this will be simple and efficient it will help with any range of whole numbers

([1-9][0-9\.]+).*

this expression will help with decimal numbers

0

You can use the following regex:

[1-9][0-9]\d|0
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Waild
  • 1
  • 2
-1
^(0{1,})?([1-9][0-9]{0,1})$

It includes: 1-99, 01-099, 00...1-

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Vickel Nov 26 '22 at 22:15