regex: 1-10999

  • Thread starter Thread starter SpaceMarine
  • Start date Start date
S

SpaceMarine

hello,

can anyone show me the ideal way to do a regular expression for

1-10999

....? Trying to validate a number.


thanks,
sm
 
can anyone show me the ideal way to do a regular expression for

1-10999

....btw i may be best off using math for this. im trying to validate a
string presented like "b752" where the "b" is optional, and the number
portion is 1-10999. i may just parse it instead, but i thought a regex
would be neat if easy.

sm
 
can anyone show me the ideal way to do a regular expression for

1-10999

...? Trying to validate a number.

Something along the lines of this:

^((?!0{1,4})\d{1,4})|(10\d{3})$


First alternative matches numbers 0-9999, and there is a negative
lookahead assertion to avoid matching any form of 0 (00, 000, 0000),
so we end up with 1-9999. Second alternative validates 10000-10999.

Of course, unless you really need a regex specifically, it will be
both faster and clearer to use int.TryParse, and do a range check on
success.
 
Hello SpaceMarine,
hello,

can anyone show me the ideal way to do a regular expression for

1-10999

...? Trying to validate a number.


In what environment are you trying to validate this input?

In asp.net, look at the range validator, it's much better than a regex.

In windows forms, use the following code instead

int tmp = -1;
bool success = int.TryParse(your_value, out tmp);
return (success && tmp >= 1 && tmp <= 10999);
 
SpaceMarine said:
...btw i may be best off using math for this. im trying to validate a
string presented like "b752" where the "b" is optional, and the number
portion is 1-10999. i may just parse it instead, but i thought a regex
would be neat if easy.

sm

Something like this should do that:

^b?([1-9]\d{0,3}|10\d{3})$

[1-9]\d{0,3} matches the numbers 1-9999.

10\d{3} matches the numbers 10000-10999.
 
...btw i may be best off using math for this. im trying to validate a
string presented like "b752" where the "b" is optional, and the number
portion is 1-10999. i may just parse it instead, but i thought a regex
would be neat if easy.

Something like this should do that:

     ^b?([1-9]\d{0,3}|10\d{3})$

[1-9]\d{0,3} matches the numbers 1-9999.

Not necessarily, depending on the requirements. It won't match the
number "0001", for example.
 
Pavel said:
SpaceMarine said:
can anyone show me the ideal way to do a regular expression for
1-10999
...btw i may be best off using math for this. im trying to validate a
string presented like "b752" where the "b" is optional, and the number
portion is 1-10999. i may just parse it instead, but i thought a regex
would be neat if easy.
sm
Something like this should do that:

^b?([1-9]\d{0,3}|10\d{3})$

[1-9]\d{0,3} matches the numbers 1-9999.

Not necessarily, depending on the requirements. It won't match the
number "0001", for example.

Yes, that's right. The requirements is not specific about that, but the
solution is... it matches the numbers written in it's most compact form,
i.e. without leading zeroes.

For a pattern that allows any number of leading zeroes, add 0* before
the number:

^b?0*([1-9]\d{0,3}|10\d{3})$
 
Not necessarily, depending on the requirements. It won't match the
number "0001", for example.

Yes, that's right. The requirements is not specific about that, but the
solution is... it matches the numbers written in it's most compact form,
i.e. without leading zeroes.

For a pattern that allows any number of leading zeroes, add 0* before
the number:

    ^b?0*([1-9]\d{0,3}|10\d{3})$

thanks guys, brilliant solutions.

i cant use TryParse because there are other parts of the value that
are strings, which i omitted from this question as ive already got
them in place.


sm

sm
 
Of course, unless you really need a regex specifically, it will be
both faster and clearer to use int.TryParse, and do a range check on
success.

yeah i was trying to use a regex so i could employ ASP.NET's regex
validator control for the client-side validation. the user input is
mixed alpha & numeric, the numeric needing to be in my valid range.

in the end i went w/ a CustomValidator control, did some parsing for
the alpha parts, and used conditionals on the numeric portion.


thanks,
sm
 
Back
Top