I want to match a number which is less than or equal to 100, it can be anything within 0-100, but the regex should not match for a number which is greater than 100 like 120, 130, 150, 999, etc.
-
2How about `0.5` or `1e2`? Should those match? What about `0000001`? – Tim Pietzcker Jun 13 '12 at 09:23
-
8Why do you want to use a regex for this? Perl already has perfectly serviceable Boolean comparison operators (<, <=, ==, >, >=). – Dave Cross Jun 13 '12 at 09:32
-
1As Dave says, are you sure you want a regular expression? Perl will treat a variable as a string or a number interchangably, and you could write `if ($num >= 0 and $num <= 100) { ... }` – Borodin Jun 13 '12 at 10:43
-
@Borodin - BTW, you can also place your condition `if($n>=0 && $n<=100)` **within** a regex - by using a **code assertion** ;-) – rubber boots Jun 13 '12 at 13:43
7 Answers
Try this
\b(0*(?:[1-9][0-9]?|100))\b
Explanation
"
\b # Assert position at a word boundary
( # Match the regular expression below and capture its match into backreference number 1
0 # Match the character “0” literally
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?: # Match the regular expression below
# Match either the regular expression below (attempting the next alternative only if this one fails)
[1-9] # Match a single character in the range between “1” and “9”
[0-9] # Match a single character in the range between “0” and “9”
? # Between zero and one times, as many times as possible, giving back as needed (greedy)
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
100 # Match the characters “100” literally
)
)
\b # Assert position at a word boundary
"

- 10,970
- 4
- 42
- 55
-
4the link you provided is restricted by requesting username and password! – M Reza Saberi Mar 08 '16 at 06:51
-
-
It fails to match **floating-points**. By the way, I've also added an [answer here](https://stackoverflow.com/questions/11011674/how-to-match-a-number-which-is-less-than-or-equal-to-100/53736645#53736645). – vrintle Dec 12 '18 at 05:43
How about this for the regex:
^([0-9]|[1-9][0-9]|100)$
this would validate 7, 82, 100 for examples, but would not validate 07 or 082.
Check this out for more information (and variations including zero prefixing) on number range checking
If you need to cater for floating point numbers you should read this, here is an expression you can use:
Floating point: ^[-+]?([0-9]|[1-9][0-9]|100)*\.?[0-9]+$

- 47,875
- 21
- 135
- 185
My practical Advice.
Personally, I would refrain writing such a complex regex altogether. What if your number changes from 100 to 200 in near future. Your regex will have to change significantly and it might be even harder to write. All the above solutions are NOT self explanatory and you will have to complement it with a comment in your code. That's a smell.
Readability matters. Code is for humans and not for machines.
Why not write some code around it and keep regex dead simple to understand.

- 12,338
- 7
- 39
- 41
Use Code Assertions if you need a regex (eventually):
/^(.+)$(??{$^N>=0 && $^N<=100 ? '':'^'})/
Test:
my @nums = (-1, 0, 10, 22, 1e10, 1e-10, 99, 101, 1.001e2);
print join ',', grep
/^(.+)$(??{$^N>=0 && $^N<=100 ? '':'^'})/,
@nums
Result:
0,10,22,1e-010,99

- 7,243
- 6
- 49
- 61

- 14,924
- 5
- 33
- 44
-
tried to see the link, but still don't understand the regexp. DO you think you can explain it ? (specially the --- ? '':'^' --- part – taiko Jun 23 '16 at 09:39
-
This looks interesting, but the link is dead and I can't find anything meaningful when googling for "code assertions". Would you mind elaborating? – Tobias Feil Dec 20 '21 at 09:41
regex for this
perl -le 'for (qw/0 1 19 32.4 100 77 138 342.1/) { print "$_ is ", /^(?:100|\d\d?)$/ ? "valid input" : "invalid input"}'

- 235,170
- 19
- 170
- 241

- 432
- 2
- 9
-
Great! The best answer. I will let it be raw here ^(?:255|\d\d?)$ – Ivan Kryvosheia Feb 21 '21 at 15:05
This regEx matches the numbers 0-100 diapason and disallow numbers like 001:
\b(0|[1-9][0-9]?|100)\b

- 12,262
- 10
- 69
- 70
this matches 0 to 100
^0*([0-9]|[1-8][0-9]|9[0-9]|100)$

- 767
- 9
- 30
-
It fails to match **floating-points**. By the way, I've also added an [answer here](https://stackoverflow.com/questions/11011674/how-to-match-a-number-which-is-less-than-or-equal-to-100/53736645#53736645). – vrintle Dec 12 '18 at 05:46
-
Also, you can combine `[1-8][0-9]|9[0-9]` into `[0-9]{2}` as both of them are two digit numbers. – vrintle Dec 12 '18 at 05:47