2

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.

Why doesn't this work as expected?

3
  • 5
    You forgot the hyphen. String regexp = "(\\d{1,})-(\\d{1,})";. See ideone.com/JeNO2u Commented Sep 30, 2015 at 20:54
  • now I see: 2 value: 15-42 value:15 value:42, but I need to have just 15 and 42. Commented Sep 30, 2015 at 20:58
  • I did not post an answer because I thought you would delete the question seeing the missing hyphen. I posted an answer because another answer was posted. Well, it is up to you to decide which one is best now. Commented Sep 30, 2015 at 21:09

3 Answers 3

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):

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

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
Sign up to request clarification or add additional context in comments.

1 Comment

Just FYI: {1,} limiting quantifier is semantically equal to + quantifier (1 or more), so you can use String regexp = "(\\d+)-(\\d+)";
1

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

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

6 Comments

Repeating what I said. But you forgot that OP does not need the zeroth group value.
@stribizhev Actually, I posted earlier, but it's not relevant.
I commented 7 minutes ago with a code demo link. You posted 2 mins ago.
When I saw your comment, it did not mention the fact that the OP did not call m.group(i). But I don't want to argue here, I can delete my answer if you want.
No, why delete if it is posted, and you have rep for it :). I seem to suffer for my SO answering style.
|
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]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.