Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex number between 1 and 100

Tags:

string

regex

I searched a lot and can't find the solution for this RegExp (I have to say I'm not very experienced in Reg. Expressions).

Regex = ^[1-9]?[0-9]{1}$|^100$ 

I would like to test a number between 1 and 100, excluding 0

like image 358
YProgrammer Avatar asked Nov 20 '12 12:11

YProgrammer


People also ask

How do you specify a number range in regex?

Example: Regex Number Range 1-20 Range 1-20 has both single digit numbers (1-9) and two digit numbers (10-20). For double digit numbers we have to split the group in two 10-19 (or in regex: "1[0-9]") and 20. Then we can join all these with an alternation operator to get "([1-9]|1[0-9]|20)".

How do I find a number in regex?

Python Regex – Get List of all Numbers from String. To get the list of all numbers in a String, use the regular expression '[0-9]+' with re. findall() method. [0-9] represents a regular expression to match a single digit in the string.

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

What is the regular expression for numbers only?

To check for all numbers in a field To get a string contains only numbers (0-9) we use a regular expression (/^[0-9]+$/) which allows only numbers. Next, the match() method of the string object is used to match the said regular expression against the input value.


2 Answers

Try:

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

Working fiddle

EDIT: IF you want to match 00001, 00000099 try

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

FIDDLE

like image 75
Ankur Avatar answered Sep 21 '22 17:09

Ankur


For integers from 1 to 100 with no preceding 0 try:

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

For integers from 0 to 100 with no preceding 0 try:

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

Regards

like image 39
bytepan Avatar answered Sep 20 '22 17:09

bytepan