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.