fork download
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class Main
  5. {
  6. public static void main(String[] args)
  7. {
  8. products("{1,3}{4,5}{6,7}");
  9. products("not valid string");
  10. }
  11. private static void products(final String products) {
  12. final String regex = "(?:\\{([0-9]+),([0-9]+)\\})";
  13.  
  14.  
  15. final java.util.regex.Pattern pAll = java.util.regex.Pattern.compile(regex+"+");
  16.  
  17. if(!pAll.matcher(products).matches()) {
  18. throw new IllegalArgumentException("Wrong semantic of products!");
  19. }
  20.  
  21. final java.util.regex.Pattern p = java.util.regex.Pattern.compile(regex);
  22. final Matcher matcher = p.matcher(products);
  23.  
  24. while(matcher.find()) {
  25. System.out.print(matcher.group(1) + " ");
  26. System.out.println(matcher.group(2));
  27. }
  28. }
  29. }
Runtime error #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
1 3
4 5
6 7