Re: regular expression question

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
Cc: Joel Rodrigues <borgempath(at)Phreaker(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: regular expression question
Date: 2003-06-05 12:03:30
Message-ID: 20030605120330.GA20878@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jun 05, 2003 at 02:05:14 -0400,
Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl> wrote:
> 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,

I don't know that it mattered for the original question, but it is surely
possible to allow only one consectutive space. There are a couple of
different ways to do this depending on exactly what you want. But probably
the most common also prohibits leading and trailing spaces and doesn't
allow an empty string. It should be easy to see how to change things for
different requirements.

'^([-0-9,]+( [-0-9,]+)*)?$'

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2003-06-05 12:18:11 Re: Weird Character Ordering
Previous Message Mike Mascari 2003-06-05 09:28:49 Re: Broken RR?