Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3451900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:11:43+00:00 2026-05-18T09:11:43+00:00

I am looking for a regular expression that will match any number from 1

  • 0

I am looking for a regular expression that will match any number from 1 to 50 inclusive. So far, I have found examples but they all allow the string to contain a decimal point, which I do not want to include. So 1,13,24,50 are OK but 1. ,etc are not. Is there a REGEXP that I can use?

Thanks in advance,
Tim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:11:43+00:00Added an answer on May 18, 2026 at 9:11 am

    Try this:

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

    UPDATE:

    Now that I see the question has been updated to refer to MySQL, this changes things significantly. The above-mentioned regular expression uses non-capturing parens which are not supported by MySQL. But it also begs the question; should you really be using regular expressions to solve this problem? We really have to look at how you are storing your numbers that must be between 1 and 50. Are they varchars? Are they ints? I’ll demonstrate how to solve it both ways. 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 put some test data into it making sure all our edge cases are covered:

    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 that 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 perform poorly on large data sets because it can’t use an index even if one is present. MySQL must run the regular expression once for every row in the table. Suppose your numbers between 1 and 50 were stored as ints in the int_number column. You could simply 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 perform well because it can use an index and it’s also more readable and more maintainable. Wins all around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a regular expression that will match all strings EXCEPT those that
Hi guys I am looking for a regular expression which will not match any
I am looking for a regular expression that will test for matches against a
I'm looking for a regular expression that removes illegal characters. But I don't know
I am looking for a Regular expression to match only if a date is
Given a regular expression, I'm looking for a package which will dynamically generate the
I'm looking for a .NET regular expression extract all the URLs from a webpage
I'm looking for a simple regular expression to match the same character being repeated
I'm looking for a regular expression that allows for either single-quoted or double-quoted strings,
I'm looking for a regular expression library in .Net that supports lazy evaluation. Note:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.