title: Why doesn't this Java regular expression work?
body: <p>I need to create a regular expression that allows a string to contain any number of:</p>
  
  <ul>
  <li>alphanumeric characters</li>
  <li>spaces</li>
  <li>(</li>
  <li>)</li>
  <li>&amp;</li>
  <li>.</li>
  </ul>
  
  <p>No other characters are permitted. I used <a href="http://www.regexbuddy.com/" rel="nofollow noreferrer">RegexBuddy</a> to construct the following regex, which works correctly when I test it within RegexBuddy:</p>
  
  <pre><code>\w* *\(*\)*&amp;*\.*
  </code></pre>
  
  <p>Then I used RegexBuddy's "Use" feature to convert this into Java code, but it doesn't appear to work correctly using a simple test program:</p>
  
  <pre><code>public class RegexTest
  {
    public static void main(String[] args)
    {
      String test = "(AT) &amp; (T)."; // Should be valid
      System.out.println("Test string matches: "
        + test.matches("\\w* *\\(*\\)*&amp;*\\.*")); // Outputs false
    }
  }
  </code></pre>
  
  <ul>
  <li>I must admit that I have a bit of a blind spot when it comes to regular expressions. Can anyone explain why it doesn't work please?</li>
  </ul>