Using special characters in regular expressions

5
Currently I am using a String.replaceAll(Regex param, String param) function in java. I know that with some cases you have to escape your caracter with a "\". For example: You can not not say: replaceAll(" ",""); but you need to use: replaceAll("\s",""); While using this function I got exceptions for the following cases: replaceAll(".",""); and replaceAll("\.",""); replaceAll("",""); and replaceAll("\",""); replaceAll(":",""); and replaceAll("\:",""); The stacktrace is as follows: Caused by: null java.util.regex.Matcher.appendReplacement(Unknown Source) java.util.regex.Matcher.replaceAll(Unknown Source) java.lang.String.replaceAll(Unknown Source) csvexport.actions.Translate.replace(Translate.java:45) csvexport.actions.Translate.translate(Translate.java:26) csvexport.actions.TranslationTemplate.executeAction(TranslationTemplate.java:35) csvexport.actions.TranslationTemplate.executeAction(TranslationTemplate.java:1) com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:60) com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:378) com.mendix.core.actionmanagement.B.B(ActionManager.java:153) com.mendix.core.Core.execute(Core.java:351) com.mendix.modules.microflowengine.B.E.B.B(JavaAction.java:62) com.mendix.modules.microflowengine.microflow.E.A(MicroflowObject.java:63) com.mendix.modules.microflowengine.microflow.Microflow.executeAction(Microflow.java:95) com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:60) com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:378) com.mendix.core.actionmanagement.B.B(ActionManager.java:153) com.mendix.core.Core.execute(Core.java:351) com.mendix.core.action.client.ExecuteAction.execute(ExecuteAction.java:131) com.mendix.externalinterface.servlet.B.A(ServletHandler.java:103) com.mendix.externalinterface.servlet.HttpServlet.A(HttpServlet.java:120) com.mendix.externalinterface.servlet.MxServlet.B(MxServlet.java:71) com.mendix.externalinterface.servlet.MxServlet.doPost(MxServlet.java:54) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) javax.servlet.http.HttpServlet.service(HttpServlet.java:820) org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502) org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:389) org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417) org.mortbay.jetty.handler.HandlerList.handle(HandlerList.java:49) org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) org.mortbay.jetty.Server.handle(Server.java:326) org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:534) org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:879) org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:747) org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218) org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228) org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:520) Does anyone know how to use the replaceAll( input, output ); function for the following inputs: ".", ":", "_", "/" so that I get the result I need?
asked
1 answers
5

You can just use the string's replace function, that is a simpler version that doesn't involve regular expressions, so you don't have to wring your hands with worry about special regular expression characters. Calling it is easy:

String result =s.replace(".", "");

And yes, it does replace all instances of '.' with 'something':

In your case your code can look something like this:

 String[] specialChars = {"|", "/", ":", "."};
 for (String specialChar : specialChars) {
      text = text.replace(specialChar, "");
 }
answered