Yet another group pattern example

If we just want to print the numbers in the below String we can create a group for those numbers. And we know that anything that falls into a group should be enclosed in brackets ()

Here what we do is we want something that starts with { then we group something using () and in group we put a . to match anything then + for more than one occurrences and then ? to stop at first occurences of } otherwise it will print everything from 0 till 2,5}

So . means any character ,+ means at least one character and ? turns it to a lazy quantifier

String challenge10= "{0,2},{0,5},{1,3},{2,5}";
        
Pattern pattern10 = Pattern.compile("\\{(.+?)\\}");

Matcher matcher10 =  pattern10.matcher(challenge10);
    while(matcher10.find()){
     System.out.println("Occurences "+matcher10.group(1));
 }

If we just want to print the numbers in the curly braces and not the alphabets we can use \d instead of . inside the group like this

  "\\{(\\d+,\\d+)\\}"

Complete code is below

   String challenge10a= "{0,2},{0,5},{1,3},{2,5},{x,y},{5,10},{20,25}";
       
    Pattern pattern10a = Pattern.compile("\\{(\\d+,\\d+)\\}");
    Matcher matcher10a =  pattern10a.matcher(challenge10a);
    while(matcher10a.find()){
        System.out.println("Occurences "+matcher10a.group(1));
     }
If we don't to print the comma , in between the digits we can further divide our groups i.e define first group with n number of digits then comma and then second group again with n number of digits.
 Pattern pattern10b = Pattern.compile("\\{(\\d+),(\\d+)\\}");
 Matcher matcher10b =  pattern10b.matcher(challenge10a);
 while(matcher10b.find()){
 // Then we print both groups 1 and 2 as we defined two groups above
 System.out.println("Occurences "+matcher10b.group(1)+" "+matcher10b.group(2));
 }