Re: Problem with a Pettern Matching Check

From: Andreas Seltenreich <andreas+pg(at)gate450(dot)dyndns(dot)org>
To: Sebastian Siewior <lavish(at)kamp-dsl(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Problem with a Pettern Matching Check
Date: 2005-08-15 22:43:28
Message-ID: 877jemg6sv.fsf@gate450.dyndns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Sebastian Siewior schrob:

> Hello hopefully correct List,

perfectly.

> I was trying to do something that is not working as it supposed to.
> First I created a table:
>
> create table t (
> col CHAR (3) CONSTRAINT numonly_col CHECK ( col ~ '^\\d+$' )
> );
>
> This check avoids non-numbers like '1a1' and allows '123'. For some
> reason, I'm unable to find out why, it also avoids things like '1' and
> '12'. Could someone please give me hint? :)

Char is padded with spaces, and that is also why your regexp is not
matching in these situations. You could either adjust your regexp to
match the trailing spaces or use varchar(3) instead:

--8<---------------cut here---------------start------------->8---
scratch=# select '1'::char(3) ~ '^\\d+$';
?column?
----------
f
(1 row)

scratch=# select '1'::char(3) ~ '^\\d+\\s*$';
?column?
----------
t
(1 row)

scratch=# select '1'::varchar(3) ~ '^\\d+$';
?column?
----------
t
(1 row)
--8<---------------cut here---------------end--------------->8---

regards
Andreas
--

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Dmitri Bichko 2005-08-15 22:44:14 Re: SUSPECT: RE: Re: Problem with a Pettern Matching Check
Previous Message Michael Fuhr 2005-08-15 22:42:08 Re: Problem with a Pettern Matching Check