A positive number from 1 to 2^31 -1 in regex - 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.

Related

Regex to check a DEFINE set of number that has "E" exponential in it

I trying find a regex which can satisfy my requirements for finding a set of numbers.
What is valid:
Number should be positive
All the digits should be 9
After or before decimal. Like, 9999.9999 9999 9999999999.9
9999.0000 9999.99000 is also valid because 0 after decimal doesn’t count
9999. is also valid for me
Number like 999999.8 999999998 99999990.9 99990000 is invalid because it has different number in it
The number should be greater than or equal to 9999
999.9999 99 is invalid number
Till now everything was working as expected with ^[9]{4,}\.[9]*[0]*$ this.
Later I found out, I may have exponential representation of that number.
Beauty with exponential number it will not have any trailing Zeros(0). So there is no chance of getting number like 1.1230000e5. This will be always be represented as 1.123e5.
Other conditions remains same. I wrote something like this ^[9]\.[9]{3,}[eE][0-9]*$
But this is not working on most of the cases.
Number 9.999E2(= 9.999 * 102 = 999.9) is invalid for me but the above regex says it's valid.
So found a pattern.
If the number of 9's after the decimal is N, power of exponent should be always be between 3 to N (inclusive)
9.99999eM : No of 9's after decimal 5. Here possible values of M are 3 (9999.99), 4 (99999.9) and 5 (999999). M can’t be 6 (9999990 invalid number).
Is there any way to modify this regrex ^[9]\.[9]{3,}[eE][0-9]*$ so that I can achieve my requirements?
Thanks in advance 🙏
You could adopt a "sledgehammer" approach
^[9].(([9]{3}[eE][3])|([9]{4}[eE][3-4])|([9]{5}[eE][3-5]))$
where the list of alternatives can be extended as appropriate.
Possible solutions depend on where the regex is being called. For example, if invoked from a language such as Perl or Python, would allow a programmed solution in conjunction with regex.

Regular Expression (consecutive 1s and 0s)

Hey I'm supposed to develop a regular expression for a binary string that has no consecutive 0s and no consecutive 1s. However this question is proving quite tricky. I'm not quite sure how to approach it as is.
If anyone could help that'd be great! This is new to me.
You're basically looking for alternating digits, the string:
...01010101010101...
but one that doesn't go infinitely in either direction.
That would be an optional 0 followed by any number of 10 sets followed by an optional 1:
^0?(10)*1?$
The (10)* (group) gives you as many of the alternating digits as you need and the optional edge characters allow you to start/stop with a half-group.
Keep in mind that also allows an empty string which may not be what you want, though you could argue that's still a binary string with no consecutive identical digits. If you need it to have a length of at least one, you can do that with a more complicated "or" regex like:
^(0(10)*1?)|(1(01)*0?)$
which makes the first digit (either 1 or 0) non-optional and adjusts the following sequences accordingly for the two cases.
But a simpler solution may be better if it's allowed - just ensure it has a length greater than zero before doing the regex check.

Regex for binary number divisible by 8 OR odd

"Write a regular expression that describes all strings of zeroes and ones representing binary numbers that are either odd or divisible by 8. The numbers may not have any leading zeros."
I gave my answer as (000|1)$ which was marked wrong. I cannot see the reason why. Please explain! Thanks in advance.
You forgot two of the requirements.
It must contain only 1s and 0s:
^[01]*(000|1)$
There may be no leading 0s:
^(?!0)[01]*(000|1)$
If lookaheads are not allowed, it becomes a bit trickier:
^1[01]*(000|1)$|^1$
Another addition, if you are allowed to use only the regular expression constructs that are available in "theoretical" regular expressions (alternation, group and repetition), it would look like this (anchors are implicit in this case):
1(0|1)*(000|1)|1

Regular Expression for number

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

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}$