fork download
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Ideone
  8. {
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. String regex = "\\S.{0,39}(?<=\\S)(?!\\S)";
  12. String string = "abcd (efghij # / klmno (# #)\n"
  13. + "blah blah etc etc words and more words and yet more words. What about these words?\n"
  14. + "And some more text for this string so that we can test things out. ";
  15.  
  16. Pattern pattern = Pattern.compile(regex);
  17. Matcher matcher = pattern.matcher(string);
  18.  
  19. while (matcher.find()) {
  20. System.out.println(matcher.group(0));
  21. }
  22. }
  23. }
Success #stdin #stdout 0.06s 33888KB
stdin
Standard input is empty
stdout
abcd (efghij # / klmno (# #)
blah blah etc etc words and more words
and yet more words. What about these
words?
And some more text for this string so
that we can test things out.