I need a regular expression to check that a string's value is either a '0', or a positive number with a length equal to 1 to 10 (also where the first digit cannot be zero).
I'm stuck, I can get the 0, but I can't get the positive number.
Here is what I have:
(^([0])$)|(^([1-9][0-9]{0-9})$)
This reg exp looks a little crazy, I've been trying a lot of different things and making even more crazier and crazier.
For a range of possibilities, you use a comma, not a hyphen.
(^([0])$)|(^([1-9][0-9]{0,9})$)
However, your regex can be shortened to:
^(0|[1-9][0-9]{0,9})$
offering the faster, non-Regex approach:
static void Main(string[] args
{
string str = "12";
long test;
if(str.Length <= 10
&& long.TryParse(str, out test)
&& test >= 0)
{
//valid
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With