fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. final String regex = "\\s*(.{30}[^.]*\\.|[^.]+$)";
  12. final String string = "The boy ate the apple. The sun is shining high in the sky. The answer to life the universe and everything is forty two, said the big computer.\n\n\n"
  13. + "The boy ate the apple. The sun is shining high in the sky. The answer to life the universe and everything is forty two, said the big computer. This is a test and this is a test\n\n"
  14. + "and this is a test.\n\n"
  15. + "This is \n\n"
  16. + "a test and then this is \n"
  17. + "a test again. Test";
  18.  
  19. Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
  20. Matcher matcher = pattern.matcher(string);
  21.  
  22. while (matcher.find()) {
  23. for (int i = 1; i <= matcher.groupCount(); i++) {
  24. System.out.println("Group " + i + ": " + matcher.group(i));
  25. }
  26. }
  27. }
  28. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
Group 1: The boy ate the apple. The sun is shining high in the sky.
Group 1: The answer to life the universe and everything is forty two, said the big computer.
Group 1: The boy ate the apple. The sun is shining high in the sky.
Group 1: The answer to life the universe and everything is forty two, said the big computer.
Group 1: This is a test and this is a test

and this is a test.
Group 1: This is 

a test and then this is 
a test again.
Group 1: Test