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.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String s = "TABLE1\n=======\n1 | 2\n15 | 2\n3 | 15\n\nTABLE2\n=======\n3 | 5\n12 | 2\n17 | 7";
  13. Pattern pattern = Pattern.compile("(?<tabledata>\\S+)\\s+\\S+(?<vals>[|\\d\\s]+)");
  14. Matcher matcher = pattern.matcher(s);
  15. List<List<String>> res = new ArrayList<>();
  16. while (matcher.find()){
  17. List<String> lst = new ArrayList<>();
  18. if (matcher.group("tabledata") != null) {
  19. lst.add(matcher.group("tabledata"));
  20. }
  21. if (matcher.group("vals") != null) {
  22. Matcher m = Pattern.compile("\\d+").matcher(matcher.group("vals"));
  23. while (m.find()) {
  24. lst.add(m.group(0));
  25. }
  26. }
  27. res.add(lst);
  28. }
  29. System.out.println(res);
  30. }
  31. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
[[TABLE1, 1, 2, 15, 2, 3, 15], [TABLE2, 3, 5, 12, 2, 17, 7]]