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?)$/
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?)$/
 
    
    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.
 
    
    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");
}
 
    
    Try ...
^(?:[1-4][0-9]{1,3}|[1-9][0-9]{0,2}|5000)$
 
    
    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:
With that said, I think it would probably be simpler to just parse the int and see if it's in range.
 
    
    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])$
