fork download
  1. import java.util.*;
  2. import java.io.*;
  3. import java.util.regex.*;
  4. import java.util.List;
  5.  
  6. class Program {
  7. public static void main (String[] args) throws java.lang.Exception {
  8.  
  9. String subject = "exp_0-a_1-b_2-c_3-d_4-exp_5";
  10. Pattern regex = Pattern.compile("a_\\d-b_\\d|c_\\d-d_\\d|(-)");
  11. Matcher m = regex.matcher(subject);
  12. while (m.find()) {
  13. if(m.group(1) != null) m.appendReplacement(b, "SplitHere");
  14. else m.appendReplacement(b, m.group(0));
  15. }
  16. m.appendTail(b);
  17. String replaced = b.toString();
  18. String[] splits = replaced.split("SplitHere");
  19. for (String split : splits) System.out.println(split);
  20. } // end main
  21. } // end Program
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
exp_0
a_1-b_2
c_3-d_4
exp_5