Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExp range of number (1 to 36)

Tags:

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

I would like to test a number between 1 and 36, excluding 0 and 37 and above.

What I've got so far and almost works (it doesn't accept 17, 18, 19, 27, 28, 29)...

^[1-9]{1}$|^[1-3]{1}[0-6]{1}$|^36$; 

Can someone help me please?

like image 787
jackJoe Avatar asked Apr 27 '11 22:04

jackJoe


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)".

Can you use regex with numbers?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.

How do you 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.

How do I match a pattern in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .


1 Answers

You know about \d, right?

^([1-9]|[12]\d|3[0-6])$ 

Try this in console:

function test() {     for(var i = 0; i < 100; i++) {         if (/^([1-9]|[12]\d|3[0-6])$/.test(i.toString()) != (i >= 1 && i <=36)) {             document.write(i + "fail");         }                 else                 document.write(i + "pass");         document.write("<br/>");     } } 
like image 104
harpo Avatar answered Sep 28 '22 11:09

harpo