fork download
  1. import java.util.regex.*;
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String s = "текст1 текст2 'текст3 текст4' текст5 'текст6' текст7";
  11. String regex = "'[^']*'";
  12. for (String l : splitIncludingDelimiters(regex, s)) {
  13. System.out.println(l);
  14. }
  15. }
  16. public static String[] splitIncludingDelimiters(String regex, String text) {
  17. List<String> list = new LinkedList<>();
  18. Matcher matcher = Pattern.compile(regex).matcher(text);
  19. int strt = 0;
  20. while(matcher.find()){
  21. list.add( text.substring(strt, matcher.start()) );
  22. list.add(matcher.group());
  23. strt = matcher.end();
  24. }
  25.  
  26. if (strt < text.length()) list.add( text.substring(strt) );
  27.  
  28. return list.toArray(new String[list.size()]);
  29. }
  30. }
Success #stdin #stdout 0.07s 33420KB
stdin
Standard input is empty
stdout
текст1 текст2 
'текст3 текст4'
 текст5 
'текст6'
 текст7