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. String regex = "(?:(START)(?=.*END)|\\G(?!^))((?:(?!START|END)(?>\\\\+\\\"|[^\\r\\n\\\"]))*)\\\"";
  12. String string = "This \" is START an \" example input END string \"\"\n"
  13. + "START This is a \"\" second example END\n"
  14. + "This\" is \"a START third example END \" \"\n\n"
  15. + "START This is a \"\"\\\"\"\\\\\\\\\\\"\"\" test \"\" test\\\" second example END \" and more \"\"\"\" END \"";
  16. String subst = "$1$2\\\\\"";
  17.  
  18. Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
  19. Matcher matcher = pattern.matcher(string);
  20.  
  21. String result = matcher.replaceAll(subst);
  22.  
  23. System.out.println(result);
  24. }
  25. }
Success #stdin #stdout 0.11s 48380KB
stdin
Standard input is empty
stdout
This " is START an \" example input END string ""
START This is a \"\" second example END
This" is "a START third example END " "

START This is a \"\"\"\"\\\\\"\"\" test \"\" test\" second example END " and more """" END "