Re: regular expression question - Mailing list pgsql-general

From Bruno Wolff III
Subject Re: regular expression question
Date
Msg-id 20030605120330.GA20878@wolff.to
Whole thread Raw
In response to Re: regular expression question  (Alvaro Herrera <alvherre@dcc.uchile.cl>)
List pgsql-general
On Thu, Jun 05, 2003 at 02:05:14 -0400,
  Alvaro Herrera <alvherre@dcc.uchile.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,]+)*)?$'

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Weird Character Ordering
Next
From: "Johnson, Shaunn"
Date:
Subject: adding comments to a table