title: Java Check string has only certain words and characters
body: <p>I'm trying to verify that a String only contains certain words and characters. </p>
  
  <p>I'm going to receive a string, and need to check that it only contains the following:</p>
  
  <ul>
  <li>"("</li>
  <li>")"</li>
  <li>" AND " Note the spaces</li>
  <li>" OR " Note the space</li>
  <li>any integer. If I can check that it's less than 10 at the same time, that would be a bonus, but I can do that separately if it complicates the code.</li>
  </ul>
  
  <p>If there is any other character or word in the string, I need to reject it.</p>
  
  <p>A valid string would be:
  (1 OR ((2 AND 3) OR 4 AND 5))</p>
  
  <p>I think a regex is the way to be going about this, but I can't work out how to build the regex to do this. </p>
  
  <p>I know the regex patterns are:</p>
  
  <ul>
  <li>\(</li>
  <li>\)</li>
  <li>\sOR\s</li>
  <li>\sAND\s</li>
  <li>\d+</li>
  </ul>
  
  <p>I've joined them together, so the pattern is:
  "\sOR\s|\sAND\s|\d+|\(|\)"</p>
  
  <p>This obviously works in that it will find any of the values, but won't reject the string if there are extra characters or strings in the string.</p>
  
  <p>I don't have to use a regex, but as I'm learning about them, this would be an ideal example for some help.
  I've been using <a href="http://www.regexplanet.com/advanced/java/index.html" rel="nofollow">http://www.regexplanet.com/advanced/java/index.html</a> to test different combinations, but have admitted defeat for now.</p>
  
  <p>Any help appreciated.</p>