Java中十进制数的正则表达式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43084537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:40:19  来源:igfitidea点击:

What is the Regex for decimal numbers in Java?

javaregexreal-number

提问by Gian Alix

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.

我不太确定什么是 Java 时期的正确正则表达式。这是我的一些尝试。可悲的是,它们都意味着任何角色。

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

我实际上想要做的是写出非负实数(允许小数)的正则表达式。所以可能性是: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.

最后一行应该打印 false 但无论如何打印 true。我认为我的正则表达式是错误的。

回答by Rizwan M.Tuman

Update

更新

To match non negative decimal number you need this regex:

要匹配非负十进制数,您需要此正则表达式:

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

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

或在 java 语法中: "^\\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 :

用 java 转义它变成:

"[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]+

回答by Teedeez

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 bracketsincluding .. 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 orthat for example this regex [\\d.]means match a single digit or a literal dot

实际上有两种方法可以匹配文字.. 一种是像您那样使用反斜杠转义\\.,另一种方法是将其包含在字符类或方括号中,例如[.]. 大多数特殊字符成为方括号内的文字字符,包括.. 因此,与您想要的只是匹配文字 dot\\.相比,use更清楚地表明了您的意图。使用如果需要匹配多个东西代表匹配这个那个比如这个表达式来匹配单个数字或文字点[.].[][\\d.]

回答by phatfingers

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 allof your examples would match a zero-length string.

您最初的理解可能是正确的,但是您被抛出了,因为在使用时matcher.find(),您的正则表达式将找到字符串中的第一个有效匹配项,并且您的所有示例都将匹配零长度字符串。

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

我会建议 "^([0-9]+\\.?[0-9]*|[0-9]*\\.[0-9]+)$"

回答by Serg Burlaka

If you on Kotlin, use ktx:

如果您使用 Kotlin,请使用 ktx:

fun String.findDecimalDigits() =
    Pattern.compile("^[0-9]*\.?[0-9]*").matcher(this).run { if (find()) group() else "" }!!