title: Regex for special characters in java
body: <pre><code>public static final String specialChars1= "\\W\\S";
  String str2 = str1.replaceAll(specialChars1, "").replace(" ", "+");
  
  public static final String specialChars2 = "`~!@#$%^&amp;*()_+[]\\;\',./{}|:\"&lt;&gt;?";
  String str2 = str1.replaceAll(specialChars2, "").replace(" ", "+");
  </code></pre>
  
  <p>Whatever <code>str1</code> is I want all the characters other than letters and numbers to be removed, and spaces to be replaced by a plus sign (<code>+</code>).</p>
  
  <p>My problem is if I use <code>specialChar1</code>, it does not remove some characters like <code>;</code>, <code>'</code>, <code>"</code>, and if I am use <code>specialChar2</code> it gives me an error :  </p>
  
  <pre><code>java.util.regex.PatternSyntaxException: Syntax error U_REGEX_MISSING_CLOSE_BRACKET near index 32:
  </code></pre>
  
  <p>How can this be to achieved?. I have searched but could not find a perfect solution.</p>