Regular Expression for number - regex

Please help me to make a Regular Expression with following rule -
Max number can be 9999.99
Should be non-negative
Can not be 0, but 0.01 and so on is valid
So, I need a non negative number between 0.01-9999.99

Erm, this isn't really a job for Regexp, but it works with it anyway:
/(\d{2,4}(\.(\d[1-9])|([1-9]\d))?)|[1-9]/
A More strict evaluation would be:
/^([1-9]\d{,3}(\.\d{1,2})?)?|(0\.([1-9]\d?)|(0\.0[1-9]))$/
With not accepting leading zero's, but allowing for just one decimal: "0.1".
Bear in mind, decimals are optional.
I suggest, however, to use mathematical operations: Convert to float and then check:
if((num > 0) && (num < 100000)) {...}
You can use sprintf() to get the representation that you need, for instance limiting the number of decimals, etc.

Why do you need a regular expression to do this? Just convert your string to a double and check if it's between 0.01 and 9999.99.

As people have already answered, you can get digits fairly easily by using [0-9] or \d. By using {min,max} you can specify how many of a character or character set to get for a match.
Here's a good reference: http://www.regular-expressions.info/reference.html

Related

What Regex do I use to specify a number below 1000000?

What would the best RegEx be for allowing the input of a number below 1000000 (1 million) with 2 decimal places? For example:
Valid:
1
1.10
999999.99 (maximum)
0.50
Invalid
1.5
1.
1000000
Essentially, a currency value below 1 million.
I've come up with the following, but it requires the decimal point and doesn't require a decimal value should the decimal point be added:
^[0-9]{1,6}\\.?[0-9]{0,2}$
You can try this. ^(\d|[1-9]\d{1,5})(\.\d{2})?$ Matches any number below 1 million, with two optional decimal places. Will not match leading zeros.
https://regex101.com/r/Hz1b3G/1
If leading zeros are allowed, use this one ^0*\d{1,6}(\.\d{2})?$
I will echo the sentiment that you should check this using your programming language after the value has been entered.
A requirement like this could of course be satisfied by "1-6 digits optionally followed by a decimal point and two more digits," but in my opinion "a regular expression is not the appropriate way to do it" because soon-enough the business will come up with a twist on this requirement that will defeat this approach. When, not if, this happens, you'll regret having tried to do it this way.
Use the regex to ensure that the input IS "a number," but not to check the characteristics of that number. Use if/then/else logic elsewhere in your application for this, and all other, data-validation rules. "Less than 1 million" is simply another rule – one of many.

Regex to validate a number with only one digit following a decimal

I have a regex which validates the user in entering a number with only one digit after a decimal.
However, if I enter: 0.1, 0.2, 0.3, ..., 0.9, it says invalid for each. How do I correct this regex to also accept the above values?
/^[1-9][0-9]{0,3}$|^[1-9][0-9]{0,3}[\.][0-9]$/
You're requiring the first digit to be 1-9.
I think what you want is even simpler than what you have there. Your regex is failing because of the [1-9] at the start since it is forcing the answer to have a 1-9 at the beginning so...
/^[0-9][0-9]{0,3}$|^[1-9][0-9]{0,3}[\.][0-9]$/
I might be mistaken but I think you could do all of this with a much more simplified regex...
/^\d+(?:\.\d)?$/
/^[0-9]{0,4}$|^[0-9]{0,4}[\.][0-9]$/
Or more succinctly
/^\d{0,4}$|^\d{0,4}[\.]\d$/
You were requiring [1-9] prior to the decimal.
Since your regex is structured to disallow leading zeros in the existing cases, you probably want to permit only one zero before the decimal point when the integer portion is zero. That means adding another alternative:
/^(?:[1-9][0-9]{0,3}$|[1-9][0-9]{0,3}\.[0-9]|0\.[0-9])$/
But what about 0 alone? Is it valid input? If so, you can make the fraction part optional in that alternative:
/^(?:[1-9][0-9]{0,3}$|[1-9][0-9]{0,3}\.[0-9]|0(?:\.[0-9])?)$/
That also allows 0.0. If you don't want that, you can change the final [0-9] to [1-9]:
/^(?:[1-9][0-9]{0,3}$|[1-9][0-9]{0,3}\.[0-9]|0(?:\.[1-9])?)$/
Also, notice how I enclosed the alternation in a group, so I only have to use one ^ and one $ for the whole regex.

A positive number from 1 to 2^31 -1 in regex

I got the following regex that almost does the work but does not exclude zero ...How to do that?
^(\d|\d{1,9}|1\d{1,9}|20\d{8}|213\d{7}|2146\d{6}|21473\d{5}|214747\d{4}|2147482\d{3}|21474835\d{2}|214748364[0-7])$
Also can anybody explain a bit how this works?
Regular expressions are not the right tool for this job. A much better solution is to extract the integer from your string (you can use a regex for this, just \d+), then convert that to an integer, then check the integer against your desired range.
An important corollary is to never blindly use a regular expression (or any code, really) that you don't understand yourself. What would you do if you used the regular expression above, then a requirement came in to modify the acceptable range?
As Greg said, regexes are not the right tool for the job here. But if you insist on knowing how the regex you pasted works:
The most important thing to remember is that 2**31 - 1 = 2147483647 (a number with 10 digits). In essence, the regex says:
The number can have 1-9 digits, OR
It can be 1 with any 9 digits after it, OR
20 with any 8 digits after it, OR
213 with any 7 digits after it, OR
... I'm sure you see where it's going
It restricts the numbers to the range of being below 2147483647.
P.S. given such a number as a string s, in Python, you can just pose this condition:
1 <= int(s) <= 2**31 - 1
In addition to the other answers, your regex doesn't work (besides allowing 0): it incorrectly excludes numbers like 2100000000, 2147483639, and most of the numbers between those two. The solution is to replace most of the nnnn prefixes with nnn[0-n] (along with other fixes), but the real solution is to not use regular expressions.

RegEx Numeric Check in Range?

I'm new to StackOverflow, so please let me know if there is a better way to ask the following question.
I need to create a regular expression that detects whether a field in the database is numeric, and if it is numeric does it fall within a valid range (i.e. 1-50). I've tried [1-50], which works except for the instances where a single digit number is preceded by a 0 (i.e. 06). 06 should still be considered a valid number, since I can later convert that to a number.
I really appreciate your help! I'm trying to learn more about regular expressions, and have been learning all I can from: www.regular-expressions.info. If you guys have recommendations of other sites to bone up on this stuff I would appreciate it!
Try this
^(0?[1-9])|([1-4][0-9])|(50)$
The idea of this regex is to break the problem down into cases
0?[1-9] takes care of the single digit case allowing for an optional preceeding 0
[1-4][0-9] takes care of all numbers from 10 to 49. This also allwows for a preceeding 0 on a single digit
50 takes care of 50
Regular expressions work on characters (in this case digits), not numbers. You need to have a separate pattern for each number of digits in your pattern, and combine them with | (the OR operator) like the other answers have suggested. However, consider just checking if the text is numeric with a regular expression (like [0-9]+) and then converting to an integer and checking the integer is within range.
You can't easily do range checking with regular expressions. You can -- with some work -- develop a pattern that recognizes a numeric range, but it's usually quite complex, and difficult to modify for a slightly different range.
You're better off breaking this into two parts.
Recognize the number pattern (^\d+$).
Check the range of that number in an application program.
^0?[1-50]{1,2}$

How can I check if at least one of two subexpressions in a regular expression match?

I am trying to match floating-point decimal numbers with a regular expression. There may or may not be a number before the decimal, and the decimal may or may not be present, and if it is present it may or may not have digits after it. (For this application, a leading +/- or a trailing "E123" is not allowed). I have written this regex:
/^([\d]*)(\.([\d]*))?$/
Which correctly matches the following:
1
1.
1.23
.23
However, this also matches empty string or a string of just a decimal point, which I do not want.
Currently I am checking after running the regex that $1 or $3 has length greater than 0. If not, it is not valid. Is there a way I can do this directly in the regex?
I think this will do what you want. It either starts with a digit, in which case the decimal point and digits after it are optional, or it starts with a decimal point, in which case at least one digit is mandatory after it.
/^\d+(\.\d*)?|\.\d+$/
Create a regular expression for each case and OR them. Then you only need test if the expression matches.
/^(\d+(\.\d*)?)|(\d*\.\d+)$/
A very late answer, but like to answer, taken from regular-expressions.info
[-+]?[\d]*\.?[\d]+?
Update This [\d]*\.?[\d]+?|[\d]+\. will help you matching 1.
http://regex101.com/r/lJ7fF4/7