Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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
RegExp可以在perl、javascript和objective-c中工作,但不能在java中工作_Java_Javascript_Regex - Fatal编程技术网

RegExp可以在perl、javascript和objective-c中工作,但不能在java中工作

RegExp可以在perl、javascript和objective-c中工作,但不能在java中工作,java,javascript,regex,Java,Javascript,Regex,我正在将代码从Javascript转换为Java,我发现Java中的正则表达式不能按预期工作(使用标准类) 它在perl、js和带有NSRegularExpression的Cocoa中运行良好 reg exp是([a-z]*)([0-9]*),java代码如下所示 它必须匹配由空格分隔的两组,第一组仅包含字母,第二组仅包含数字 public static void main(String[] args) { Matcher matcher = Pattern.compile("([a-z]*)

我正在将代码从Javascript转换为Java,我发现Java中的正则表达式不能按预期工作(使用标准类)

它在perl、js和带有NSRegularExpression的Cocoa中运行良好

reg exp是
([a-z]*)([0-9]*)
,java代码如下所示

它必须匹配由空格分隔的两组,第一组仅包含字母,第二组仅包含数字

public static void main(String[] args) {
Matcher matcher = Pattern.compile("([a-z]*) ([0-9]*)").matcher("hello 101");
while (matcher.find()) {
    for (int i = 0; i < matcher.groupCount(); i++) {
        System.out.println(i + ": " + matcher.group(i));
    }
}
publicstaticvoidmain(字符串[]args){
Matcher Matcher=Pattern.compile(([a-z]*)([0-9]*)).Matcher(“hello 101”);
while(matcher.find()){
对于(int i=0;i
}

数字组永远不会返回。
怎么了?

您过早地结束了
for
循环:

for (int i = 0; i <= matcher.groupCount(); i++) {
//                ^^   
    System.out.println(i + ": " + matcher.group(i));
}

for(int i=0;我的天啊!!我从未意识到groupCount返回2,我(错误地)预期为3!!非常感谢