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 rx = "(?sm)(\\G(?!\\A)|^sufixpart$)(?:(?!^(?:sufixpart|end)$).)*?(subdata\\S*)(?=.*?^end$)";
  13. String s = "invalidsufix\nsubadatax\nsufixpart\nsubdata1\nsomerandomn\nsubdata2\nsubdatan\nend\ninvalidsufix\nsubadatax\nsufixpart\nsubdata001\nsomerandomn\nsubdata002\nsubdata00n\nend";
  14. Pattern pattern = Pattern.compile(rx);
  15. Matcher matcher = pattern.matcher(s);
  16. List<List<String>> res = new ArrayList<>();
  17. List<String> lst = null;
  18. while (matcher.find()){
  19. if (!matcher.group(1).isEmpty()) {
  20. if (lst != null) res.add(lst);
  21. lst = new ArrayList<>();
  22. lst.add(matcher.group(2));
  23. } else lst.add(matcher.group(2));
  24. }
  25. if (lst != null) res.add(lst);
  26. System.out.println(res);
  27. }
  28. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
[[subdata1, subdata2, subdatan], [subdata001, subdata002, subdata00n]]