Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式匹配号模式_Java_Regex_Number Formatting - Fatal编程技术网

Java正则表达式匹配号模式

Java正则表达式匹配号模式,java,regex,number-formatting,Java,Regex,Number Formatting,我想检查我的号码是否与正则表达式模式匹配 我想要实现的是检查一个数字是否与模式匹配,然后执行代码 我的if声明如下: public String refactorNumber(String input){ if(input.matches("#,##0.##")) { //execute code } } 但它从不匹配,我输入的数字是: - 0 - 100 - 1,100.01 我做错了什么?恐怕您还没有正确理解正则表达式语法 从示例代码来看,您似乎在尝试

我想检查我的号码是否与正则表达式模式匹配

我想要实现的是检查一个数字是否与模式匹配,然后执行代码

我的if声明如下:

public String refactorNumber(String input){
    if(input.matches("#,##0.##")) {
       //execute code
    }
}
但它从不匹配,我输入的数字是:

 - 0
 - 100
 - 1,100.01

我做错了什么?

恐怕您还没有正确理解正则表达式语法

从示例代码来看,您似乎在尝试匹配一个数字,后跟一个逗号,后跟两个数字,后跟一个零,后跟一个小数点,后跟两个数字

为此,正则表达式模式需要:

\d,\d{2}0\.\d{2}
了解模式的一个很好的资源是这个正则表达式备忘单:

不幸的是,该网站目前存在问题,因此您可以使用此google搜索来查找:

您可以这样做(如果我做错了,请纠正我):


下面是一行简单的代码,我只想用来检测数字

if (input.matches("[0-9]*") {
//code here
}

用于什么?这不是一个正则表达式。#=number但不是mandatory老实说,我认为您应该仔细阅读String.matches()#=number但不是强制->的文档,以及
matches()
应该如何理解*您自己的语法?
if (input.matches("[0-9]*") {
//code here
}