I need a regex in Java that will check if a String representation of a double has required 6 decimal places. Before the decimal point, value can be positive or negative.
1.123456 - correct
-123123123.123456 - correct
123123123.123456 - correct
-123123123.123456 - correct
1.12345 - wrong
-.123456 - wrong
.123456 - wrong
.12345 - wrong
123456 - wrong
I tried:
^\s*(?=.*[1-9])\d*(\.\d{6})?\s*$
but it doesn't cover all edges.
Try this:
^\s*(-|\+)?(0|[1-9]\d*)\.\d{6}\s*$
See live demo.
This allows the first digits to be zero only if it's the only digit before the dot, eg 0.123456 is OK, but not 01.123456. \.\d{6} requires exactly 6 decimal places.
The valid input should
start with optional whitespaces --->^\s*
then optional - or +--->(-|\+)?
then one or multiple digits--->\d+
then one dot ---> .
then six digits --->(\d{6})
end with optional whitespaces --->^\s*
Try this:
^\s*(-|\+)?\d+\.(\d{6})\s*$
In your regex the positive lookahead (?=.*[1-9]) asserts that what is on the right side should contain a digit which will succeed for all examples. After that assertion you match zero or more digits \d* followed by a part that optionally matches a dot and 6 digits (\.\d{6})? so this will match .588888 or also 1.
If you want to match an optional minus sign you could use -?
For your example data you might use:
^-?\d+\.\d{6}$
In Java:
String regex = "^-?\\d+\\.\\d{6}$";
Explanation
^ Assert the start of the line
-? Match an optional minus sign
\d+\.\d{6} Match one or more digits, a dot and 6 digits
$ Assert the end of the line
Demo
Related
I want to have a number validation. Rules are:
A number can start with + or - or nothing (it is taken as a positive number)
Cannot start with 0
Can have a fraction either . or ,
Cannot end with 0
So acceptable numbers are: +123, -123, 123, 1023, 123.03, 123,03.
Non acceptable numbers are: 001, 1.000, any letters
I give you the expression that I ve built so far, on Dan's Tools. I have managed almost everything, except expressions after the the fraction. Every help is acceptable.
Expression: (^(\+|-?)([1-9]+))([0-9]+)(\.|,?)
Thanks in advance
Nikos
Except the fractional part that is missing in your pattern, your regex won't match single digit numbers as you quantified [1-9] and [0-9] with + quantifier requiring at least one char.
You can use
^[+-]?[1-9][0-9]*(?:[.,][0-9]*[1-9])?$
See the regex demo and the regex graph:
Details
^ - start of string
[+-]? - an optional + or -
[1-9] - a single non-zero digit
[0-9]* - zero or more digits
(?:[.,][0-9]*[1-9])? - an optional fractional part: . or , and then zero or more digits followed with a single non-zero digit
$ - end of string.
I have the following regex but it fails (the inner digits with the points) :
([0-9]{1,3}\.?[0-9]{1,3}\.?[0-9]{1,3})
I want that it covers the following cases:
123 valid
123.4 valid
123.44 valid
123.445 valid
123.33.3 not ok (regex validates it as true)
123.3.3 not ok (regex validates it as true)
123.333.3 valid
123.333.34 valid
123.333.344 valid
Can you please help me?
You have multiple case, I would like to use | the or operator like this :
^([0-9]{1,3}|[0-9]{1,3}\.[0-9]{1,3}|[0-9]{1,3}\.[0-9]{3}\.[0-9]{1,3})$
^ ^ ^ ^
you can check the regex demo
details
The regex match three cases :
case 1
[0-9]{1,3}
this will match one or more digit
case 2
[0-9]{1,3}\.[0-9]{1,3}
this will match one or more digit followed by a dot then one or more digits
case 3
[0-9]{1,3}\.[0-9]{3}\.[0-9]{1,3}
this will match one or more digit followed by a dot then three digits then a dot then one or three digits
Note you can replace [0-9] with just \d your regex can be :
^(\d{1,3}|\d{1,3}\.\d{1,3}|\d{1,3}\.\d{3}\.\d{1,3})$
How about this one (demo at Regex101). It's pretty short and straightforward Regex:
(^\d{3}\.\d{3}\.\d{1,3}$)|(^\d{3}\.\d{1,3}$)|(^\d{3}$)
This recognizes three valid separate groups.
(^\d{3}\.\d{3}\.\d{1,3}$) as a group which must have 3 digits, a dot, 3 more digits, a dot and 1-3 digits.
(^\d{3}\.\d{1,3}$) as a group which must have 3 digits, a dot and 1-3 digits.
(^\d{3}$) as a group which must have 1-3 digits.
These groups split with the or (|) statement.
However, since you have tagged java, why don't let Java to take some responsibility and help Regex where isn't strong? I would rather match the format ((?:\d{1,3}\.?)+) and check programmatically whether the count of numbers is valid.
Use the following expression with .matches:
s.matches("\\d{1,3}(?:\\.\\d{3})?(?:\\.\\d{1,3})?")
See the regex demo
Details
^ - implicit, not necessary as the pattern is used in .matches that requires a full string match
\d{1,3} - 1 to 3 digits
(?:\.\d{3})? - an optional . and 3 digits
(?:\.\d{1,3})? - an optional sequence of . and 1 to 3 digits
$ - implicit, not necessary since the pattern is used in .matches that requires a full string match
How can I write a regular expression that matches a string with the following properties?:
Contains numbers as well as only dot which is decimal separator (But dot is not necessary which means it can be 123 or 123.4 or 123.56).
No leading dot (Not .12).
Leading zero can be written only if it is followed by a dot (can not be like 000.12, only 0.12).
Have only 2 decimal places.
To the left of the decimal point you want a number (1 or more digits) that doesn't start with a zero:
[1-9][0-9]*
Or it can be just a zero:
0|[1-9][0-9]*
The value may have a decimal point and 1-2 digits after the decimal point:
\.[0-9]{1,2}
Left side is required. Decimal point and fractional digits are optional:
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
The first non-capturing group is needed to limit the scope of the | pattern. The second non-capturing group is needed to make combined "decimal point and fractional digit" pattern optional.
Note that this will allow trailing zeroes, e.g. 100.00
Depending on preference, [0-9] can also be written as \d. I'd normally use \d, but since regex also has [1-9], I liked [0-9] better here as I felt it helped clarify the difference.
Depending on how regex is used, you may need to add the ^ begin / $ end anchors. They are needed when using find(), and are not needed when using matches() but don't hurt:
^(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?$
Using negative look-ahead to ensure the string doesn't start with zero and another digit (but can be just zero, or zero followed by a dot)
^(?!0\d)\d+(?:\.\d{1,2})?$
Explanation and sample: https://regex101.com/r/7ymqcn/1
P.S. Also more efficient than Andreas' answer (takes fewer steps to match)
You could try converting the input string to float (that would take care of removing leading zeros and the dot without zero, as well adding ".00" if the input is only an integer, and you can set the maximum decimal numbers. If an exception is thrown at the conversion, the input doesn't match what you want. You can then convert the float back to string or keep it as a float value for calculation.
See:
https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#valueOf(java.lang.String)
I want to replace numbers in a string if it is more than 3 digits (Phone numbers should be replaced) and it should not replace the number if it is followed by $ and if the number has decimal points. I used the below expression.
"\d{3,}+(?!\$/\.)"
Issues I face are , it is replacing numbers that are more than ten digits as i want to replace some numbers which are some ID's with more than 10 digits. Also if a number has more than 3 digits after the decimal , those numbers are also getting replaced. I dont want a number to be replaced if it has decimal points. can some body help?
For Eg, say a number string "3452678916381914". Actually it has to be replaced. But the above regex not replacing that. For numbers like $1234,45.567 - those numbers shouldn't be replaced. But above regex replacing 45.567
use lookahead and lookbehind regex, 1st assert start word boundary is not precede by a $ or ., then assert end word boundary is not follow by a $ or .
It works for both example you provided, you might need to tweak a little bit to handle some corner case
(?<![\$\.])\b\d{3,}\b(?![\$\.])
see demo, it match the first 2 but not the rest
3452678916381914 # match
1234 56789 # match
$1234,45.567
$1234
12.345
12345.6678
123$
I'm trying to construct a single regex (for Java) to truncate trailing zeros past the decimal point. e.g.
50.000 → 50
50.500 → 50.5
50.0500 → 50.05
-5 → -5
50 → 50
5.5 → 5.5
Idea is to represent the real number (or integer) in the most compact form possible.
Here's what I've constructed:
^(-?[.0-9]+?)\.?0+$
I'm using $1 to capture the truncated number string.
The problem with the pattern above is that 50 gets truncated to 5. I need some way to express that the 0+ must follow a . (decimal point).
I've tried using negative-behind, but couldn't get any matches.
The best solution could be using built-in language-specific methods for that task.
If you cannot use them, you may use
^(-?\d+)(?:\.0+|(\.\d*?)0+|\.+)?$
And replace with $1$2.
See the regex demo. Adjust the regex accordingly. Here is the explanation:
^ - start of string
(-?\d+) -Group 1 capturing 1 or 0 minus symbols and then 1 or more digits
(?:\.0+|(\.\d*?)0+|\.+)? - An optional (matches 1 or 0 times due to the trailing ?) non-capturing group matching 3 alternatives:
\.0+ - a decimal point followed with 1+ zeros
(\.\d*?)0+ - Group 2 capturing a dot with any 0+ digits but as few as possible and matching 1+ zeros
\.+ - (optional branch, you may remove it if not needed) - matches the trailing dot(s)
$ - end of string.
Java demo:
String s = "50.000\n50\n50.100\n50.040\n50.\n50.000\n50.500\n50\n-5";
System.out.println(s.replaceAll("(?m)^(-?\\d+)(?:\\.0+|(\\.\\d*?)0+|\\.+)?$", "$1$2"));
// => [50, 50, 50.1, 50.04, 50, 50, 50.5, 50, -5]
For a general regex which should do the trick:
^\d+?0*\.??\d*?(?=0*?[^\d]*$)
You can replace the caret and dollar sign with whatever your boundaries should be. Those could be replaced by whatever you would expect around your number.
basically:
/d+? non-greedy match for any number (needs at least 1 number to start the match)
\.*?? optional match for a decimal. Prefers to match 0 occurrences
\d*? (?=0*?[^\d]*$) - non-greedy match for a number, but would stop at the 0 which is proceeded by a non-number
EDIT: I just realized the original expression also trimmed the last zero on integers, this should work. I added the option 0 match to catch that