fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. String str = "0 - Amount: 3 - Class 29\n1 - Amount: 2 - Class 21\n2 - Amount: 11 - Class 1";
  15.  
  16. Pattern pattern = Pattern.compile("Amount: (\\d*) - Class (\\d*)");
  17. Matcher matcher = pattern.matcher(str);
  18.  
  19. StringBuffer sb = new StringBuffer("{");
  20. while (matcher.find())
  21. {
  22. sb.append(String.format("{%s, %s},", matcher.group(1), matcher.group(2)));
  23. }
  24. sb.setLength(sb.length()-1); // remove last comma
  25. sb.append("}");
  26.  
  27. System.out.println(sb.toString());
  28.  
  29. }
  30. }
Success #stdin #stdout 0.11s 321600KB
stdin
Standard input is empty
stdout
{{3, 29},{2, 21},{11, 1}}