title: Validation of data that 'closely resembles' a where clause
body: <p>I am working on validation logic of user input to what is pretty close to a sql where clause.</p>
  
  <p>On the page the user can select from a combination of dropdowns (where the '-' character represents a blank character in the final clause):</p>
  
  <pre><code>DropDown1   DropDown2       DropDown3   DropDown4   DropDown5  DropDown6
  
  -           -               -           -           -          -
  (           SomeCriteria    =           Value       )          AND
                              &lt;&gt;                                 OR
  </code></pre>
  
  <p>Unfortunately I don't have any hard requirements on how to validate this clause other than it should 'behave like a sql where clause' :(</p>
  
  <p>A few that should pass validation</p>
  
  <pre><code>(Criteria = Value)
  (criteria1 = Value1 OR Criteria2 &lt;&gt; value2)
  (criteria1 = value1) AND (Criteria2 = value2)
  (criteria1 = value1) OR (Criteria2 = value2 AND criteria3 = value3)
  </code></pre>
  
  <p>A few that should fail validation:</p>
  
  <pre><code>)Criteria = value) (first character is not  '(' )
  (Criteria = value(  (last character is not  ')' )
  ((Criteria = value)  (# of '(' does not match # of ')' )
  
  (Criteria = Value (AND Criteria1 = value1))  ( 'AND' and 'OR' should not be immediately preceded  or followed by a '('  or ')' character.
  </code></pre>
  
  <p>Any assistance on writing a regex to validate these selections would be truly appreciated.  After poking around I have seen others speak of a sql parser would that be a better solution for this 'restricted' where clause?</p>