Re: regular expression question

From: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
To: Joel Rodrigues <borgempath(at)Phreaker(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: regular expression question
Date: 2003-06-05 06:05:14
Message-ID: 20030605060514.GH3268@dcc.uchile.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jun 05, 2003 at 12:10:18AM +0530, Joel Rodrigues wrote:
> Hi,
> I'm trying to write a check on a column restricting the data to
> numerals from 0-9, commas, dashes '-' and single spaces. To allow the
> entry of something like "134-140, 310-312".
>
> I seem to have got everything but the single spaces.
>
> '^[0-9|,-]{0,10}$'

I don't think you can do what you want with a single regular expression.
You'll probably need to use multiple constraints: one to check for the
valid characters and the maximum length, and another to check that there
aren't two spaces together,

regression=# create table dos (a text, constraint dos_a_uno check (a ~
'^[a-z ]{0,10}$'), constraint dos_a_dos check (a !~ ' '));
CREATE TABLE
regression=# insert into dos values ('aaba');
INSERT 17091 1
regression=# insert into dos values ('aaba ');
INSERT 17092 1
regression=# insert into dos values ('aaba ');
ERROR: ExecInsert: rejected due to CHECK constraint "dos_a_dos" on "dos"

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"En las profundidades de nuestro inconsciente hay una obsesiva necesidad
de un universo lógico y coherente. Pero el universo real se halla siempre
un paso más allá de la lógica" (Irulan)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Blair Robertson 2003-06-05 06:50:01 Weird Character Ordering
Previous Message Hans-Jürgen Schönig 2003-06-05 05:52:24 Re: SAP and MySQL ...