title: Java Regular Expression - allow only certain characters and digits
body: <p>I need to write regular expression that allows only digits, characters like &amp; | . ( ) and spaces.</p>
  
  <pre><code>import java.util.*;
  import java.lang.*;
  import java.io.*;
  
  /* Name of the class has to be "Main" only if the class is public. */
  class Ideone
  {
      public static void main (String[] args) throws java.lang.Exception
      {
  
          List&lt;String&gt; input = new ArrayList&lt;String&gt;();
          input.add("(0.4545 &amp;&amp; 0.567) || 456"); // should PASS
          input.add("9876-5-4321");
          input.add("987-65-4321 (attack)");
          input.add("(0.4545 &amp;&amp; 0.567) || 456 &amp;&amp; (me)");
          input.add("0.456 &amp;&amp; 0.567"); // should PASS
          for (String ssn : input) {
              boolean f = ssn.matches("^[\\d\\s()&amp;|.]$");
              if (f) {
                  System.out.println("Found good SSN: " + ssn);
              }else {
                  System.out.println("Nope: " + ssn);
              }
          }
      }
  }
  </code></pre>
  
  <p>None above passed, why?</p>