67

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

Gonzo345
  • 1,133
  • 3
  • 20
  • 42
YProgrammer
  • 855
  • 3
  • 8
  • 14
  • 2
    Why not parse as number, and test for `(x >= 1) AND (x <= 100)`? – Martijn Nov 20 '12 at 12:48
  • U can use a simple if condition. – Srinivas B Nov 20 '12 at 12:49
  • 6
    dont solve this using regex... – user219882 Nov 20 '12 at 12:49
  • 1
    possible duplicate of [regular expression for csv numbers from 1 to 1000](http://stackoverflow.com/questions/12909137/regular-expression-for-csv-numbers-from-1-to-1000) – lanzz Nov 20 '12 at 12:52
  • @Martijn: One reason is that ASP.NET webforms validation controls use offer RegEx-based validation. And this would not only ensure numeric input but a range for input of a percentage with a single validation control. – Daniel Przybylski May 27 '15 at 22:29
  • @DanielPrzybylski: ASP.NET webform validation controls also include *RangeValidator*, which would be far more appropriate in this case. – Martijn May 29 '15 at 09:37
  • 1
    I hit this question because I'm dealing with a 3rd party xml in a programming app, and there's no choice but to use regex. ASP.NET was an example, but not the only one so it doesn't matter if there's a better choice in THAT problem space. – loctrice Apr 01 '16 at 16:46

13 Answers13

121

Try:

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

Working fiddle

EDIT: IF you want to match 00001, 00000099 try

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

FIDDLE

Ankur
  • 12,676
  • 7
  • 37
  • 67
16

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

bytepan
  • 361
  • 2
  • 5
13

Here are very simple regex's to understand (verified, and no preceding 0)

Between 0 to 100 (Try it here):

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

Between 1 to 100 (Try it here):

^([1-9][0-9]?|100)$
Mercury
  • 7,430
  • 3
  • 42
  • 54
10

Try it, This will work more efficiently.. 1. For number ranging 00 - 99.99 (decimal inclusive)

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

Working fiddle link

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

2.For number ranging 1-100(inclusive) with no preceding 0.

(?:\b|-)([1-9]{1,2}[0]?|100)\b

Working Fiddle link

http://regex101.com/r/mN1iT5/6

Tim Elsass
  • 966
  • 7
  • 9
Ved
  • 11,837
  • 5
  • 42
  • 60
  • Check it on fiddle. Link is there. – Ved Aug 14 '14 at 10:38
  • I have edited the expression. There was slight mistake. Check it now. – Ved Aug 14 '14 at 10:45
  • 1
    I think it would be more helpful for the OP and further visitor, when you add some explanation to your intension. – Reporter Aug 14 '14 at 11:02
  • Hmm. Still not - it won't match 100 and will match (for example) 11.33 – GHC Aug 14 '14 at 14:09
  • This expression is for numbers 1 to 99 including decimal numbers. – Ved Aug 18 '14 at 04:24
  • In its current form, your regex matches numbers in the range **00 to 99.99**. The OP said he wanted to match numbers from **1 to 100** (inclusive), and he didn't say anything about decimals. I'm pretty sure he didn't want the leading zeroes, either. – Alan Moore Aug 18 '14 at 06:51
  • Thank you @AlanMoore for suggestion. I have edited my expression. Hope this will as per your expectation. – Ved Aug 18 '14 at 07:43
  • 1
    ...and then some! Correct **and** creative; gets an upvote from me. – Alan Moore Aug 18 '14 at 08:29
  • the first regex doesn't match the number 20 – Tim Elsass Jul 29 '17 at 17:37
  • @Ved I have small request, can I have regular expression in between 1 to 100, accept decimal value of maximum two. example 0.24,1.99 ,9.99 99.99 are valid result. Invalid are 00.24, 000.25, 0.245, 999.000 are invalid – kiran Jul 16 '18 at 23:29
3

If one assumes he really needs regexp - which is perfectly reasonable in many contexts - the problem is that the specific regexp variety needs to be specified. For example:

egrep '^(100|[1-9]|[1-9][0-9])$'
grep -E '^(100|[1-9]|[1-9][0-9])$'

work fine if the (...|...) alternative syntax is available. In other contexts, they'd be backslashed like \(...\|...\)

Alex North-Keys
  • 4,200
  • 1
  • 20
  • 22
3

There are many options how to write a regex pattern for that

  • ^(?:(?!0)\d{1,2}|100)$
  • ^(?:[1-9]\d?|100)$
  • ^(?!0)(?=100$|..$|.$)\d+$
Ωmega
  • 42,614
  • 34
  • 134
  • 203
1

I use for this angular 6

try this.

^([0-9]\.[0-9]{1}|[0-9]\.[0-9]{2}|\.[0-9]{2}|[1-9][0-9]\.[0-9]{1}|[1-9][0-9]\.[0-9]{2}|[0-9][0-9]|[1-9][0-9]\.[0-9]{2})$|^([0-9]|[0-9][0-9]|[0-99])$|^100$

it's validate 0.00 - 100. with two decimal places.

hope this will help

<input matInput [(ngModel)]="commission" type="number" max="100" min="0" name="rateInput" pattern="^(\.[0-9]{2}|[0-9]\.[0-9]{2}|[0-9][0-9]|[1-9][0-9]\.[0-9]{2})$|^([0-9]|[0-9][0-9]|[0-99])$|^100$" required #rateInput2="ngModel"><span>%</span><br>

Number should be between 0 and 100

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
aruna
  • 21
  • 4
  • 1
    more accurate one ^([0-9]\.[0-9]{1}|[0-9]\.[0-9]{2}|\.[0-9]{2}|[1-9][0-9]\.[0-9]{1}|[1-9][0-9]\.[0-9]{2}|[0-9][0-9]|[1-9][0-9]\.[0-9]{2})$|^([0-9]|[0-9][0-9]|[0-99])$|^100$ – aruna Mar 24 '20 at 09:42
  • This works perfectly for me where the answer's expression allows for 3+ decimal places. – TaeKwonJoe Aug 10 '21 at 01:35
0

Regular Expression for 0 to 100 with the decimal point.

^100(\.[0]{1,2})?|([0-9]|[1-9][0-9])(\.[0-9]{1,2})?$
Radadiya Nikunj
  • 988
  • 11
  • 10
0

Just for the sake of delivering the shortest solution, here is mine:

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

working fiddle

Aleksandar
  • 1,496
  • 1
  • 18
  • 35
0

1 - 1000 with leading 0's

/^0*(?:[1-9][0-9][0-9]?|[1-9]|1000)$/;

it should not accept 0, 00, 000, 0000.

it should accept 1, 01, 001, 0001

Gaslan
  • 808
  • 1
  • 20
  • 27
0

The simplest one

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

Edit

The explanation detail
[1-9] matches between 1 and 9
[0-9]? is optional one between 0 and 9
| means OR
100 means 100

The combination matches between 1 and 100.

Juzbird
  • 41
  • 3
-1

between 0 and 100

/^(\d{1,2}|100)$/

or between 1 and 100

/^([1-9]{1,2}|100)$/
abSiddique
  • 11,647
  • 3
  • 23
  • 30
  • 1
    This isn't a very good answer. The first version matches 0, which the question says not to match. The second version doesn't match two-digit numbers. – Kenster Feb 02 '18 at 18:52
-6

This is very simple logic, So no need of regx.

Instead go for using ternary operator

var num = 89;
var isValid = (num <=  100 && num > 0 ) ? true : false;

It will do the magic for you!!

wogsland
  • 9,106
  • 19
  • 57
  • 93
  • 1
    Except for where you need a regex. For instance, say an xml template that is predefined, where the only way to limit input is by putting a regex in the template. Then there's no magic here. – loctrice Apr 01 '16 at 16:42