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