Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for numeric range from negative numbers?

Tags:

regex

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.

like image 238
jon kra Avatar asked Feb 17 '11 02:02

jon kra


People also ask

How do you match a number range in regex?

\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.

Does decimal allow negative number?

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.

Can you use regex for numbers?

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.


2 Answers

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*)?$
like image 77
pcofre Avatar answered Oct 27 '22 17:10

pcofre


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.

like image 35
xkrz Avatar answered Oct 27 '22 19:10

xkrz