title: How to use regex with PostgreSQL to Constrain a Columns input as alpha-numeric
body: <p>I am attempting to create a constraint on a table that will ensure that a columns input is alpha-numerical ( eventually wishing to add a constraint that will prevent certain inputs based on the context type)</p>
  
  <p>The following code does not seem to work because the <code>regexp_matches()</code> function needs to return a <code>boolean</code>. How would I go about using Regex in a column constraint so it returns a <code>boolean</code> value for <code>CHECK</code>.</p>
  
  <pre><code>CREATE TABLE contexts (
    id INT PRIMARY KEY,
    name VARCHAR NOT NULL,
    short_name VARCHAR NOT NULL,
    context_type VARCHAR REFERENCES context_types ON DELETE RESTRICT,
    context_status VARCHAR REFERENCES context_statuses ON DELETE RESTRICT,
    parent_id INT REFERENCES contexts ON DELETE CASCADE,
  
    CONSTRAINT CK_name CHECK (regexp_matches(name, '[a-zA-Z0-9]*')),
    CONSTRAINT CK_short_name CHECK (regexp_matches(short_name, '[a-zA-Z0-9]*'))
  );
  </code></pre>
  
  <p>.</p>
  
  <p>[SOLVED]
  As the answer I have shows that the best way to use regex with CHECK is to use the Regular Expression Match Operators ( <a href="http://www.postgresql.org/docs/9.5/static/functions-matching.html" rel="nofollow">mentioned in this documentation</a> -- see section 9.7.3)</p>