fork 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.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. final Pattern p = Pattern.compile("\"(?:\\\\.|[^\"\\\\])*\"");
  14. String testString = "\"Hey Topaz how are you?\" Nathan continues to take a seat and proceeds with, \"I hate that guy\"";
  15. StringBuilder message = new StringBuilder(testString);
  16. final Matcher m = p.matcher(testString);
  17. int cnt = 0;
  18. int replaceCount = 0;
  19. while (m.find()) {
  20. int startIndex = m.start();
  21. int endIndex = m.end() + 1;
  22. System.out.println("Start: " + startIndex);
  23. System.out.println("End: " + endIndex);
  24. message.insert(startIndex + replaceCount*2, 'C');
  25. message.insert(endIndex + replaceCount*2, 'D');
  26. replaceCount += 1;
  27. }
  28. System.out.println(message);
  29. }
  30. }
Success #stdin #stdout 0.12s 320576KB
stdin
Standard input is empty
stdout
Start: 0
End: 25
Start: 76
End: 94
C"Hey Topaz how are you?"D Nathan continues to take a seat and proceeds with, C"I hate that guy"D