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 - Fatal编程技术网

Java 正则表达式正向查找困难

Java 正则表达式正向查找困难,java,regex,Java,Regex,我的目标是匹配第一个0以及该0之后的所有十进制值。如果第一个小数位是零,那么我也要匹配小数位。如果没有小数,则不捕获任何内容 以下是我想要的一些例子: 180.570123 // should capture the "0123" on the end 180.570 // should capture the "0" on the end 180.0123 // should capture the ".0123" on the end 180.0 // should ca

我的目标是匹配第一个0以及该0之后的所有十进制值。如果第一个小数位是零,那么我也要匹配小数位。如果没有小数,则不捕获任何内容

以下是我想要的一些例子:

180.570123 // should capture the "0123" on the end
180.570    // should capture the "0" on the end
180.0123   // should capture the ".0123" on the end
180.0      // should capture the ".0" on the end
180123     // should capture nothing
180        // should capture nothing
如果第一个小数位为0,则很容易进行匹配:

(\.0.*)
我的问题是当第一个小数位不是0时匹配。我相信积极的回顾会解决这个问题,但我无法让它正常工作。下面是我尝试过的一个正则表达式:

(?<=^.*\..*)0.*

另外,我正在测试这些正则表达式,您可以使用以下表达式:

\.[1-9]*(0\d*)
而你想要的将是在第一个捕获组中。(小数点除外。)

如果还想捕获小数点,可以使用:

(?:\.[1-9]+|(?=\.))(\.?0\d*)
示例():

输出:

180.570123  : Match: 0123
180.570     : Match: 0
180.0123    : Match: .0123
180.0       : Match: .0
180123      : Match: n/a
180         : Match: n/a
180.2030405 : Match: 030405

向后看可能有点过分了。这个很有效-->


/\.\d*(0\d*)/

我将编写一个小函数来代替正则表达式进行提取

private String getZeroPart(final String s) {
        final String[] strs = s.split("\\.");
        if (strs.length != 2 || strs[1].indexOf("0") < 0) {
            return null;
        } else {
            return strs[1].startsWith("0") ? "." + strs[1] : strs[1].substring(strs[1].indexOf("0"));
        }
    }
输出:

0123
0
.0123
.0
null
null
030405
null
180.570123 -> 180.57
180.570 -> 180.57
180.0123 -> 180
180.0 -> 180
180123 -> 180123
180 -> 180
180.2030405 -> 180.2
180.5555 -> 180.5555
更新

基于问题的编辑。对方法进行一些更改以获得正确的编号:

private String cutZeroPart(final String s) {
    final String[] strs = s.split("\\.");
    if (strs.length != 2 || strs[1].indexOf("0") < 0) {
        return s;
    } else {
        return strs[1].startsWith("0") ? strs[0] : s.substring(0, strs[0].length() + strs[1].indexOf("0") + 1);
    }
}
您可以尝试以下方法:

        Matcher m = Pattern.compile("(?<=(\\.))(\\d*?)(0.*)").matcher("180.2030405");
        String res="";
        if(m.find()){
            if(m.group(2).equals("")){
                res = "."+m.group(3);
            }
            else{
                res = m.group(3);
            }
        }

Matcher m=Pattern.compile(“(?这不正确地捕获了案例1,根本没有捕获案例2或4。@typoknig,已更新。案例1始终正确匹配。使用第一个捕获组的内容,而不是整个匹配。啊,我明白了。正如您所说,使用第一个捕获组的内容时,它似乎确实起作用。非常感谢!这不正确地捕获了案例1而且根本不捕获案例2、3或4。但捕获小数点和以下0的情况并非如此。您希望在这里看到什么:
180.0407
该组捕获您想要的内容,除了.0案例。谢谢。有选项很好,但我认为正则表达式的方式更简洁:)
private String cutZeroPart(final String s) {
    final String[] strs = s.split("\\.");
    if (strs.length != 2 || strs[1].indexOf("0") < 0) {
        return s;
    } else {
        return strs[1].startsWith("0") ? strs[0] : s.substring(0, strs[0].length() + strs[1].indexOf("0") + 1);
    }
}
180.570123 -> 180.57
180.570 -> 180.57
180.0123 -> 180
180.0 -> 180
180123 -> 180123
180 -> 180
180.2030405 -> 180.2
180.5555 -> 180.5555
        Matcher m = Pattern.compile("(?<=(\\.))(\\d*?)(0.*)").matcher("180.2030405");
        String res="";
        if(m.find()){
            if(m.group(2).equals("")){
                res = "."+m.group(3);
            }
            else{
                res = m.group(3);
            }
        }