YProgrammer
YProgrammer

Reputation: 905

Regex number between 1 and 100

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

Upvotes: 71

Views: 271150

Answers (13)

Juzbird
Juzbird

Reputation: 51

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.

Upvotes: 0

Mercury
Mercury

Reputation: 8008

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)$

Upvotes: 15

aruna
aruna

Reputation: 21

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

Upvotes: 1

figuarnieri
figuarnieri

Reputation: 19

between 0 and 100

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

or between 1 and 100

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

Upvotes: -1

Avik Chanda
Avik Chanda

Reputation: 1

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

Upvotes: 0

Aleksandar
Aleksandar

Reputation: 1776

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

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

working fiddle

Upvotes: 0

Radadiya Nikunj
Radadiya Nikunj

Reputation: 1085

Regular Expression for 0 to 100 with the decimal point.

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

Upvotes: 0

bytepan
bytepan

Reputation: 391

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

Upvotes: 18

Ved
Ved

Reputation: 12113

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

Upvotes: 9

user2550916
user2550916

Reputation:

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!!

Upvotes: -6

Ankur
Ankur

Reputation: 12784

Try:

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

Working fiddle

EDIT: IF you want to match 00001, 00000099 try

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

FIDDLE

Upvotes: 126

Ωmega
Ωmega

Reputation: 43703

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

  • ^(?:(?!0)\d{1,2}|100)$
  • ^(?:[1-9]\d?|100)$
  • ^(?!0)(?=100$|..$|.$)\d+$

Upvotes: 3

Alex North-Keys
Alex North-Keys

Reputation: 4373

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 \(...\|...\)

Upvotes: 3

Related Questions