3

I would like to use a regex for a textbox that allows only numbers between 1-5000

I've tried the following but it wont work:

@"/^(?:1|5000|-[1-9]\d?)$/
invidicult
  • 306
  • 2
  • 8
  • 19
  • 10
    Why not just parse the string and see if its in range?.. – Sayse Sep 13 '16 at 14:57
  • @ThomasAyoub Or [_Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems._](http://regex.info/blog/2006-09-15/247) ;) – juharr Sep 13 '16 at 15:05
  • 1|5000 will literally match 1 *or* 5000, *not* anything in the range 1 - 5000. – EJoshuaS - Stand with Ukraine Sep 13 '16 at 15:26
  • If you have determined that a regex is *required*, can you give us the context in which it is used? – Andrew Morton Sep 13 '16 at 15:48
  • 1
    No need asking at SO if there are such well-known services as [this one](http://gamon.webfactional.com/regexnumericrangegenerator/) or [this](https://www.richie-bendall.ml/ros-regex-numeric-range-generator/), or [this one](https://www.myregextester.com/index.php), also see [how to do that programmatically](https://github.com/voronind/range-regex) in Python. – Wiktor Stribiżew Sep 16 '20 at 09:05

5 Answers5

7

You can use ^(?:[1-9]|\d{2,3}|[1-4]\d{3}|5000)$. But you'd better to parse to Int then do simple maths.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
5

With some parsing beforehand, you can make the regex very simple:

string s = textBox1.Text;
string r = "";
int n = 0;
if (int.TryParse(s, out n) && (n>=1 && n<=5000))
{
    r = "y";
}

if (Regex.IsMatch(r, "y")) {
    // input was valid
    MessageBox.Show("OK");
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
3

Try ...

^(?:[1-4][0-9]{1,3}|[1-9][0-9]{0,2}|5000)$

haggisandchips
  • 523
  • 2
  • 11
2

You can do something like the following:

^(([1-4][0-9]{0,3})|([1-9][0-9]{0,2})|(5000))$

The first two groups will match anything in the range of 1 - 4999. You add the |5000 at the end to make it match the range 1 - 5000. The three cases here are:

  • The number is exactly 5000
  • The number is between 1 and 3 digits. In this case, it can't possibly be more than 5000. However, the first number must be 1 - 9 so that you can't get something like "009" or "000."
  • The number is 4 digits, in which case it must be between 1000 - 4999

With that said, I think it would probably be simpler to just parse the int and see if it's in range.

-1

You can try something like this (0-366)

 ^(0?[0-9]?[0-9]|[1-2][0-9][0-9]|3[0-5][0-9]|36[0-6])$
Bela Vizy
  • 1,133
  • 8
  • 19
  • 1
    Best to give an answer for the specific question instead of a specific answer for a similar question. – juharr Sep 13 '16 at 15:01
  • 3
    How does this answers the question? – Thomas Ayoub Sep 13 '16 at 15:04
  • @ThomasAyoub I was wondering the same thing, I voted to close this for not being an answer. – EJoshuaS - Stand with Ukraine Sep 13 '16 at 15:50
  • Let me re-read the question for ya: "I would like to use a regex for a textbox that allows only numbers between 1-5000" This is an example that "allows" numbers from 0-366. – Bela Vizy Sep 13 '16 at 21:23
  • Would you consider an answer that gave a Regex for range 0-1 a good answer for this question? – Thomas Ayoub Sep 14 '16 at 14:53
  • No. You point is? Look. We don't write college text books here. We don't have to post a solution for the exact problem. It doesn't even have to work. We point people in the right direction. We help. Also it's not fair to downvote the question because our instict tells us that it may not be a good idea to do it. There's no such thing as bad question. – Bela Vizy Sep 14 '16 at 15:30