Reputation: 1435
I am looking for a regular expression that validates only positive numbers(integers):
0-999 and first number not 0.
My example not work:
string pattern = @"^\d+$";
How decided positive numbers pattern?
Upvotes: 2
Views: 22168
Reputation: 7668
If you just want to validate an input, why not using TryParse?
Regular Expression for positive numbers in C#
double result = 0;
if (Double.TryParse(myString, out result))
{
// Your conditions
if (result > 0 && result < 1000)
{
// Your number
}
}
Upvotes: 2
Reputation: 2091
You can use a lot of useful regex tools online like http://gskinner.com/RegExr/
there's a lot of ready to use examples from which you can start to get your own!
Upvotes: 0
Reputation: 2553
You could force the first digit to be 1-9, and then have any or no digits follow it, like so;
string pattern = @"^[1-9]\d*$";
You can also restrict the amount of digits by putting a numbered constraint on it.
string pattern = @"^[1-9]\d{0,2}$";
The top one will accept any positive integer >0, while the bottom one will accept only 1-999.
Upvotes: 17