What is the Regex for decimal numbers in Java?
Asked Answered
S

5

9

I am not quite sure of what is the correct regex for the period in Java. Here are some of my attempts. Sadly, they all meant any character.

String regex = "[0-9]*[.]?[0-9]*";
String regex = "[0-9]*['.']?[0-9]*";
String regex = "[0-9]*["."]?[0-9]*";
String regex = "[0-9]*[\.]?[0-9]*";
String regex = "[0-9]*[\\.]?[0-9]*";
String regex = "[0-9]*.?[0-9]*";
String regex = "[0-9]*\.?[0-9]*";
String regex = "[0-9]*\\.?[0-9]*";

But what I want is the actual "." character itself. Anyone have an idea?

What I'm trying to do actually is to write out the regex for a non-negative real number (decimals allowed). So the possibilities are: 12.2, 3.7, 2., 0.3, .89, 19

String regex = "[0-9]*['.']?[0-9]*";
Pattern pattern = Pattern.compile(regex);

String x = "5p4";
Matcher matcher = pattern.matcher(x);
System.out.println(matcher.find());

The last line is supposed to print false but prints true anyway. I think my regex is wrong though.

Salver answered 29/3, 2017 at 4:44 Comment(5)
In all of your examples you've made it optional with ?. Is that what you want? The period is just \. you don't need brackets or anything else.Left
try System.out.println("scary.wombat".replaceAll("\\.", "_"));Kovacs
If all you want is a period, why are there numbers around it?Ossiferous
please mind the difference between matcher.find() and matcher.matches() - find() does not work against the complete region applied.Porshaport
The best answer, rather a research article is here: https://mcmap.net/q/102024/-decimal-or-numeric-values-in-regular-expression-validationOxpecker
B
11

Update

To match non negative decimal number you need this regex:

^\d*\.\d+|\d+\.\d*$

or in java syntax : "^\\d*\\.\\d+|\\d+\\.\\d*$"

String regex = "^\\d*\\.\\d+|\\d+\\.\\d*$"
String string = "123.43253";

if(string.matches(regex))
    System.out.println("true");
else
    System.out.println("false");

Explanation for your original regex attempts:

[0-9]*\.?[0-9]*

with java escape it becomes :

"[0-9]*\\.?[0-9]*";

if you need to make the dot as mandatory you remove the ? mark:

[0-9]*\.[0-9]*  

but this will accept just a dot without any number as well... So, if you want the validation to consider number as mandatory you use + ( which means one or more) instead of *(which means zero or more). That case it becomes:

[0-9]+\.[0-9]+
Balaklava answered 29/3, 2017 at 4:46 Comment(0)
T
2

If you on Kotlin, use ktx:

fun String.findDecimalDigits() =
    Pattern.compile("^[0-9]*\\.?[0-9]*").matcher(this).run { if (find()) group() else "" }!!
Tisbee answered 1/3, 2019 at 15:25 Comment(0)
V
1

Your initial understanding was probably right, but you were being thrown because when using matcher.find(), your regex will find the first valid match within the string, and all of your examples would match a zero-length string.

I would suggest "^([0-9]+\\.?[0-9]*|\\.[0-9]+)$"

Vas answered 29/3, 2017 at 5:34 Comment(0)
R
0

There are actually 2 ways to match a literal .. One is using backslash-escaping like you do there \\., and the other way is to enclose it inside a character class or the square brackets like [.]. Most of the special characters become literal characters inside the square brackets including .. So use \\. shows your intention clearer than [.] if all you want is to match a literal dot .. Use [] if you need to match multiple things which represents match this or that for example this regex [\\d.] means match a single digit or a literal dot

Recurrent answered 29/3, 2017 at 4:56 Comment(0)
C
0

I have tested all the cases.

public static boolean isDecimal(String input) {
        return Pattern.matches("^[-+]?\\d*[.]?\\d+|^[-+]?\\d+[.]?\\d*", input);
}
Coligny answered 7/9, 2021 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.