Java:正则表达式无法按预期工作

时间:2021-07-10 23:51:45

I have the following piece of code:

我有以下代码:

String range = "(15-42)";
String regexp = "(\\d{1,})(\\d{1,})";

Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(range);

m.find();

System.out.println(m.groupCount());

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

And then I have the following output:

然后我有以下输出:

2
value: 15
value: 1
value: 5

But I'm only expecting to see 2 values: 15 and 42.

但我只期望看到2个值:15和42。

Why doesn't this work as expected?

为什么这不能按预期工作?

3 个解决方案

#1


1  

You need to add hyphen to the regex and use .group(i) and start with index 1 (because m.group(0) is the whole match value that you do not need):

你需要在正则表达式中添加连字符并使用.group(i)并从索引1开始(因为m.group(0)是你不需要的整个匹配值):

String range = "(15-42)";
String regexp = "(\\d{1,})-(\\d{1,})";
Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(range);
if (m.find()) {
    System.out.println(m.groupCount());
    for(int i=1; i<=m.groupCount(); i++){
        System.out.println("value:" + m.group(i));
    }
}

See IDEONE demo

请参阅IDEONE演示

Now, you will have

现在,你将拥有

2               // This is the number of capturing groups
value:15        // This is the value of the first capturing group
value:42        // This is the value of the second capturing group

#2


1  

The mistake is that you are always calling m.group() when you should be calling m.group(i).

错误是当你应该调用m.group(i)时,你总是调用m.group()。

The other mistake is that you forgot the hyphen in your regex.

另一个错误是你忘记了正则表达式中的连字符。

Working code:

工作代码:

String range = "(15-42)";
String regexp = "(\\d{1,})-(\\d{1,})";

Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(range);
m.find();
for (int i = 0; i <= m.groupCount(); i++) {
    System.out.println("value:" + m.group(i));
}

This prints the expected:

这打印出预期的:

value:15-42
value:15
value:42

#3


0  

Its a different method but you can use this solution

它是一种不同的方法,但您可以使用此解决方案

    System.out.println(m.groupCount());
    String[] value = m.group().split("-");

    for(int i=0; i<value.length; i++){
    System.out.println("value:" + value[i]);
    }

#1


1  

You need to add hyphen to the regex and use .group(i) and start with index 1 (because m.group(0) is the whole match value that you do not need):

你需要在正则表达式中添加连字符并使用.group(i)并从索引1开始(因为m.group(0)是你不需要的整个匹配值):

String range = "(15-42)";
String regexp = "(\\d{1,})-(\\d{1,})";
Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(range);
if (m.find()) {
    System.out.println(m.groupCount());
    for(int i=1; i<=m.groupCount(); i++){
        System.out.println("value:" + m.group(i));
    }
}

See IDEONE demo

请参阅IDEONE演示

Now, you will have

现在,你将拥有

2               // This is the number of capturing groups
value:15        // This is the value of the first capturing group
value:42        // This is the value of the second capturing group

#2


1  

The mistake is that you are always calling m.group() when you should be calling m.group(i).

错误是当你应该调用m.group(i)时,你总是调用m.group()。

The other mistake is that you forgot the hyphen in your regex.

另一个错误是你忘记了正则表达式中的连字符。

Working code:

工作代码:

String range = "(15-42)";
String regexp = "(\\d{1,})-(\\d{1,})";

Pattern p = Pattern.compile(regexp);
Matcher m = p.matcher(range);
m.find();
for (int i = 0; i <= m.groupCount(); i++) {
    System.out.println("value:" + m.group(i));
}

This prints the expected:

这打印出预期的:

value:15-42
value:15
value:42

#3


0  

Its a different method but you can use this solution

它是一种不同的方法,但您可以使用此解决方案

    System.out.println(m.groupCount());
    String[] value = m.group().split("-");

    for(int i=0; i<value.length; i++){
    System.out.println("value:" + value[i]);
    }