I want to validate a number using regex.. the condition is number can be from any negative 3 digit values to positive 3 digit.. but cannot be zero.
can someone please help me to get regex condition for this.
\d for single or multiple digit numbers To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.
If UNSIGNED is used with DECIMAL , negative values are not allowed. MySQL stores numbers in DECIMAL columns as strings. Therefore, numbers outside the maximum numeric range for this data type may be stored in a DECIMAL column.
The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.
There it´s It can or not start with the negative symbol, followed by a number between 1 and 9 ant then can be 0 to 2 of any number.
^\-?[1-9]\d{0,2}$
Update: And the following regex also allows decimals
^\-?[1-9]\d{0,2}(\.\d*)?$
Assuming you are dealing with whole integers, and your number don't mix with other texts, you can try this
^-?[1-9][0-9]{0,2}$
I agree with Anon, it is much better to read the String, and convert it to int to do the comparison, if you have predictable inputs.
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