fork(16) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. // your special characters
  9. String regex = "+ – && || ! ( ) { } [ ] ^ ” ~ * ? : \\";
  10. // building a valid regex out of above
  11. regex = '(' + regex.replaceAll("([^\\s]{1,2})(?=(?:\\s+|$))",
  12. "\\\\Q$1\\\\E").replace(' ', '|') + ')';
  13.  
  14. // your string to be replaced
  15. String str = "content:you&&me";
  16. // actual replacement
  17. str = str.replaceAll(regex, "\\\\$1");
  18.  
  19. // printing the result
  20. System.out.printf("********* replaced: [%s]%n", str);
  21.  
  22. }
  23. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
********* replaced: [content\:you\&&me]