Regex number between 1 and 100
RegexStringRegex Problem Overview
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
Regex Solutions
Solution 1 - Regex
Try:
^[1-9][0-9]?$|^100$
EDIT: IF you want to match 00001, 00000099 try
^0*(?:[1-9][0-9]?|100)$
Solution 2 - Regex
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
Solution 3 - Regex
Try it, This will work more efficiently..
-
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
Solution 4 - Regex
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)$
Solution 5 - Regex
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 \(...\|...\)
Solution 6 - Regex
There are many options how to write a regex pattern for that
^(?:(?!0)\d{1,2}|100)$
^(?:[1-9]\d?|100)$
^(?!0)(?=100$|..$|.$)\d+$
Solution 7 - Regex
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>
Solution 8 - Regex
Regular Expression for 0 to 100 with the decimal point.
^100(\.[0]{1,2})?|([0-9]|[1-9][0-9])(\.[0-9]{1,2})?$
Solution 9 - Regex
Just for the sake of delivering the shortest solution, here is mine:
^([1-9]\d?|100)$
Solution 10 - Regex
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
Solution 11 - Regex
This seems a better solution to use if
statement :
num = 55
if num <= 100 and num >= 1:
print("OK")
else:
print("NOPE")
Solution 12 - Regex
between 0 and 100
/^(\d{1,2}|100)$/
or between 1 and 100
/^([1-9]{1,2}|100)$/
Solution 13 - Regex
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!!