Normally I'd complain that you didn't use Google, but the regex documentation isn't exactly fantastic, so I'll let you off.
This code works (although I didn't do ALL the characters for you):
-
public class replaceAll {
-
-
/* this code has been fully tested, and is operated by "java replaceAll your string here" from the
-
* terminal/command prompt.
-
* code provided courtesy of TheScripts.com
-
*/
-
public static void main(String[] args) {
-
String s = "";
-
for (int i = 0; i < args.length; i++) {
-
s += args[i];
-
}
-
System.out.println(args.length == 0 ? "Please enter a string." : new replaceAll().replace(s));
-
}
-
-
public static String replace(String s) {
-
String returned;
-
/* important - strings are immutable (look it up) so can't just do s.replaceAll()
-
* also, if you want a regex character (such as [, {) you have to escape it with "\\"
-
* and if you want a "\" you have to replace it with "\\\\".
-
*/
-
returned = s.replaceAll("\\[","\\\\[");
-
returned = returned.replaceAll("\\{","\\\\{");
-
returned = returned.replaceAll("\\]","\\\\]");
-
returned = returned.replaceAll("\\}","\\\\}");
-
/* and so on.... */
-
-
return returned;
-
}
-
}
-