Regular expression for 1-50 without decimal point

I am looking for a regular expression that matches any number from 1 to 50 inclusive. So far I have found examples, but all of them allow you to contain a string with a decimal point, which I do not want to include. So 1,13,24,50 is fine, but 1. etc. No. Is there a REGEXP I can use?

Thanks in advance Tim

+4
source share
5 answers

Try the following:

/^(?:[1-9]|[1-4][0-9]|50)$/ 

UPDATE:

Now that I see that the question has been updated to reference MySQL, this makes a big difference. The above regex uses non-capture parens that are not supported by MySQL. But this also raises the question; if you really use regular expressions to solve this problem? We really need to see how you store your numbers, which should be from 1 to 50. Are they varchar s? Are they int s? I will demonstrate how to solve this in both directions. First, I'll set up a test table with indexes:

 create table regextest ( id int unsigned primary key auto_increment, varchar_number varchar(5) not null, int_number int not null, index(varchar_number), index(int_number) ) engine=innodb; 

Now add some test data to it, making sure that all our edge cases are closed:

 insert into regextest (varchar_number, int_number) values ('0', 0), ('1', 1), ('35', 35), ('49', 49), ('50', 50), ('51', 51); 

And now, here is a query that will solve your problem, assuming your numbers are stored as strings in the varchar_number column:

 mysql> select * from regextest where varchar_number rlike '^([1-9]|[1-4][0-9]|50)$'; +----+----------------+------------+ | id | varchar_number | int_number | +----+----------------+------------+ | 2 | 1 | 1 | | 3 | 35 | 35 | | 4 | 49 | 49 | | 5 | 50 | 50 | +----+----------------+------------+ 4 rows in set (0.00 sec) 

This works, but it will not work well on large datasets because it cannot use the index, even if it is present. MySQL should run a regex once for each row of the table. Suppose your numbers from 1 to 50 were stored as int in an int_number column. You could just do this:

 mysql> select * from regextest where int_number between 1 and 50; +----+----------------+------------+ | id | varchar_number | int_number | +----+----------------+------------+ | 2 | 1 | 1 | | 3 | 35 | 35 | | 4 | 49 | 49 | | 5 | 50 | 50 | +----+----------------+------------+ 4 rows in set (0.00 sec) 

This query will work well, as it can use an index as well as more readable and more convenient. Wins around.

+6
source
 '^(0?\d|[1-4]\d|50)$' 

I.e:

  • Start of input
  • followed by one digit (optionally preceding 0) or
  • followed by 1-4, followed by any number, or
  • and then 50
  • and then make sure that we see the end of the input.

Edit: The above allows you to use 0 (and 00), which you certainly don't want. So, if you really do not want the leading zeros to resolve anyway:

 '^([1-9]|[1-4]\d|50)$' 

Edit:. As comments from later versions of OP show that this is for MySQL, I changed the syntax to specify the template.

+4
source

^ ([1-9] | [1-4] [0-9] | 50) $

+1
source

Cheat with Perl6 :: Rules :

 / ( \d+ ) { 1 <= $1 && $1 <= 50 or fail } / 

Or in Perl 5.10 and higher,

 /(\d+)(?(?{1>$1||50<$1})(*FAIL))/ 
0
source

try this one

 ([0-4]{1}[0-9]{0,1}[.]{0,1}[0-9]{0,2}|50) 

will perform the following actions

 45.00 - Match 4 - Match 78 - Not Match 51 - Not Match 21.25 - Match 
0
source

All Articles