Regex to match only one even decimal digit - java

I am building on the idea of [Jonah][1]: Matching to one decimal place in Java using regex
^([1-9]\d*|0)(\.\d)?$
This regex is the one I want. But my additional requirement is that the decimal digit must be an even number such as 0.2, 10.4, 100.6, 11.8.
I am thinking of splitting the string at '.' and checking if decimal digit % 2 = 0. How can I achieve this in regex?

Instead of using the /d for a decimal digit, you can just explicitly require an even number, like this:
^([1-9]\d*|0)(\.[24680])?$

Related

Java regex that handles several possibilites

I am trying to find a regex for the following user generated possibilities:
÷2%3%x#4%2$#
OR
÷2%x#4%2$#
OR
÷2%x#4$#
OR
÷2%x#
To understand the expression, it is a fraction whose numerator lies between
the ÷ and the first %, and the denominator lies from first % to the #.
But, the denominator has an exponent, which lies from the # to $.
The user can input whatever number he/she desires, but the structure stays the same. Notice that the number can also be a decimal.
The structure is as follows: ÷(a number, if its two or more digits a % will be in between the digits)x(a group that consists of a number(s), also the symbols # , $ and a %(s) which can also alternate between the digits)#
Remember, the number can be a decimal number.
I am trying to use the following regex with no success:
"[÷]-?\\d+(\\.\\d*)?[%](-?\\d+(\\.\\d*)?){0,1}[x]([#]-?\\d+(\\.\\d*)?[$]){0,1}[#]"
I think that the group (-?\d+(\.\d*)?){0,1} is complicating things up.
Also, I have not accounted for the % within this group which could occur.
any suggestions, thank you
Edit: Deleted the old post content.
According to your new testcases I improved your regex to match all cases and simplified the regex:
÷[0-9%]+?x(#[0-9%]+?\$)?# OR ÷[\d%]+?x(#[\d%]+?\$)?#
Note:
The [] mark groups of allowed characters so it has no use to have the parenthesis.
Also [÷][0-9]+[0-9[%]]+? is just the same as ÷[0-9]+[0-9%]+? the first part in your example matches any number 0-9 n-times and then you check for either (0-9 or %) for n-times (non greedy fetching). So instead you can just use the second check for the whole thing.
By wrapping the exponent in a regex-class: () we can make the whole exponent optional with ? ==> this will make your 4th test-case work.
You could also substitute 0-9 with \d (any digit) if you want.
I found a regex that works, I tested from the bottom up:
Here it is:
[÷][0-9[%][\\.]]+?[x][0-9[%][\\.][#][$]]*?[#]
This regex works for all types of cases. Even those that include decimal numbers, or not exponents.
the group [0-9[%][\.][#][$]]*? allows the regex to search for exponent, which can occur zero(that's why the * is there) or more times and the ? makes it optional. Similarly, I followed the same idea for the coefficient of x(read the post if you don't know where the coefficient lies) and the numerator. Thank you for everyone that put effort in brainstorming this problem. I have chosen to use my answer for my programming.

Java - Regex for oracle NUMBER(10,8) field

I have an Oracle column of data type NUMBER(10,8). I need to validate the input data via java regex before the storing the data in tables. As per oracle's data type, valid values include:
10 digits
2 digits . 8 digits
3 digits . 7 digits
4 digits . 6 digits
no digits . 8 digits (is saved in Oracle as 0.12345678 but the input value can be like .12345678)
and so on. Negative values of these cases are also valid.
I can write regex for one case at a time. i.e we can check for 1234567891 with one regex. Then with changes in the range, we can write respective regex for all the possible combinations of the scale.
My sample regex : ^-?\\d{0,2}(?>\\.\\d{1,8})?$ : checks for 2 digits . 8 digits case.
Now I want to know, is there any easier way of checking all such values in one regex? One can always use a '|' operator but then the total number of such OR regex would be equal to the scale part of the data type.
Is there any elegant possible solution? Any pointers, suggestions are welcome!
UPDATE :
After #Andreas pointed out the actual meaning of (10,8), the question does seem to be misguided. Removing the invalid cases from the above mentioned, the valid cases are :
(0/1/2 digits).(0/1/2/../8 digits)
0/1/2 digits
negative cases
You've misunderstood the meaning of NUMBER(10,8):
Specify a fixed-point number using the following form:
NUMBER(p,s)
where:
p is the precision, or the maximum number of significant decimal digits, where the most significant digit is the left-most nonzero digit, and the least significant digit is the right-most known digit. [...]
s is the scale, or the number of digits from the decimal point to the least significant digit. [...]
It means maximum 10 significant decimal digits, with 8 digits from the decimal point, i.e. 2.8 only. The scale is not floating. Sure, you can have fewer on each side of the decimal point, but no mote than 2 on the left and 8 on the right.
Oracle names this a fixed-point number, and it is very distinct from a floating-point number, which uses the same keyword but without limits, i.e. NUMBER.
As for Oracle Database number literal format, the format is:
If you exclude scientific notation, that means a regex of:
^[+-]?(?:\d{1,2}(?:\.\d{0,8})?|\.\d{1,8})$
EDIT
Please note that this answer was provided prior to the OP's update and is no longer correct. I will leave this here in case it helps any future readers, but at the time of writing this edit Andreas' answer seems to be correct.
This may be easier to achieve by simply splitting the string on . and then testing each side's length, but this can still be achieved using regex alone.
See this regex in use here
^(?=[\d.]{1,10}$)(?:\d+(?:\.\d+)?|\.\d{1,8})$
^ Assert position at the start of the line
(?=[\d.]{1,10}$) Positive lookahead to ensure the line has a maximum of 10 characters and is composed of only digits and ..
(?:\d+(?:\.\d+)?|\.\d{1,8}) Match either of the following options
\d+(?:\.\d+)? Match any digit one or more times, optionally followed by a decimal point . and more digits (one or more). You can change \.\d+ to \.\d* to match numbers like 1. that don't have decimal numbers specified.
\.\d{1,8} Match . followed by a digit between 1-8 times. Since the 0 is implied here, it's actually the 9th digit for this number (the dot being the tenth).
$ Assert position at the end of the line
For matching the possibility of + or - at the start of the number, the following may be used.
See regex in use here
^(?=[-+\d.]{1,10}$)(?:[-+]?\d+(?:\.\d+)?|\.\d{1,8}|[+-]\.\d{1,7})$
Regex is a chomsky language of typ-3, which can not calculate something. You can only or-combine all possible formats, which results in a long and unmaintainable regex. So the easiest solution is a functional check.

Regex for numbers between 0 and 180 and decimals places in Javacc

So I'm creating a token in JavaCC by using regex.
I'm trying to only allow 3 digit numbers and is only between 0 - 180.
Also, I'm trying to only allow (in a separate token) 2 digit numbers between 0 and 59.9999 (4 decimal places).
I have no idea how to create the regex for these two tokens in JavaCC...
Any help would with an explanation would be awesome thanks :)
For the first case, your pattern needs to allow 1-digit numbers, 2-digit numbers, 3-digit numbers whose first digit is 1 and whose second digit is in the range 0-7, and the special case 180. The regex would look like
[0-9]{1,2}|1[0-7][0-9]|180
(I don't know javacc, so I don't know how this regex would be used, or whether you need something else to prevent something like 1800 from being parsed as a number, or as two numbers. You might need \b on the ends to indicate a word boundary, but I have no idea how javacc works.)
For the second case, the part to the left of the decimal point is either one digit, or two digits where the first digit is in the range 0-5. Your requirements aren't clear, but if the token is required to have a decimal point and one to four digits to the right of the decimal point, the regex would be
([0-9]|[0-5][0-9])\.[0-9]{1,4}
Again, I don't know how javacc handles the word boundaries.
Note that if this were a Java program, I would recommend (in the first case) just parsing it as an integer and comparing it to 0 and 180. Too many questioners try to use regexes to solve every problem, but they are not suited for every problem. Since this is for javacc, it may be a context in which regexes are simple to use and numeric comparisons are not--as I've mentioned, I don't know anything about javacc.

Regular expression to check if a String is a positive natural number

I want to check if a string is a positive natural number but I don't want to use Integer.parseInt() because the user may enter a number larger than an int. Instead I would prefer to use a regex to return false if a numeric String contains all "0" characters.
if(val.matches("[0-9]+")){
// We know that it will be a number, but what if it is "000"?
// what should I change to make sure
// "At Least 1 character in the String is from 1-9"
}
Note: the string must contain only 0-9 and it must not contain all 0s; in other words it must have at least 1 character in [1-9].
You'd be better off using BigInteger if you're trying to work with an arbitrarily large integer, however the following pattern should match a series of digits containing at least one non-zero character.
\d*[1-9]\d*
Debuggex Demo
Debugex's unit tests seem a little buggy, but you can play with the pattern there. It's simple enough that it should be reasonably cross-language compatible, but in Java you'd need to escape it.
Pattern positiveNumber = Pattern.compile("\\d*[1-9]\\d*");
Note the above (intentionally) matches strings we wouldn't normally consider "positive natural numbers", as a valid string can start with one or more 0s, e.g. 000123. If you don't want to match such strings, you can simplify the pattern further.
[1-9]\d*
Debuggex Demo
Pattern exactPositiveNumber = Pattern.compile("[1-9]\\d*");
If you want to match positive natural numbers, written in the standard way, without a leading zero, the regular expression you want is
[1-9]\d*
which matches any string of characters consisting only of digits, where the first digit is not zero. Don't forget to double the backslash ("[1-9]\\d*") if you write it as a Java String literal.
I made the following regex for only positive natural numbers:
^[1-9]\d*$
This will will check if a number starts with 1 to 9 (so there can't be any zero's in the beginning) and there rest of the numbers need to be numbers from 0 to 9. You can test it at https://regex101.com

Regular expression for floating point number Java

I want to ask Regular expression
I have a a float number called 0.11,
in this case, the first digit must be Zero
and there can be at most three decimal digits , like 0.1, 0.11, 0.111
In Java, I code like this
String phone_regex = "d{1,1}.d{1,3}";
But it does not work...
Can someone provide some suggestion for me?
thank you
Try escaping your digit character group \\d, e.g.,
"^0\\.\\d{1,3}$"
This matches all strings beginning with a 0, followed by a dot and 1-3 digits.
^ means beginning of line, $ means end of line. See also java.util.Pattern.
You need to escape the numbers class (\d) and the . char.
String phone_regex = "\\d\\.\\d{1,3}";

Categories