Re: regular expression question - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: regular expression question
Date
Msg-id 20030605060514.GH3268@dcc.uchile.cl
Whole thread Raw
In response to regular expression question  (Joel Rodrigues <borgempath@Phreaker.net>)
Responses Re: regular expression question  (Bruno Wolff III <bruno@wolff.to>)
List 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)

pgsql-general by date:

Previous
From: Blair Robertson
Date:
Subject: Weird Character Ordering
Next
From: "Coby Beck"
Date:
Subject: create or replace for aggregate function