Re: Regular Expression Data Type

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Richard Doust <rdoust(at)mac(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regular Expression Data Type
Date: 2007-04-21 16:29:51
Message-ID: B42A8EFE-D695-4051-B778-E97B1C34BEA9@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Apr 21, 2007, at 11:01 , Richard Doust wrote:

> select price from shipping_prices where shipFromZip = '23773' and
> shipToZip ~ '87927'
>
> because shipToZip is defined as a regular expression, I'd match a
> row where shipToZip held the value '879[0-9]{2,2}' or '87[0-9]*'.
>
> Wouldn't that be cool? Does anyone know whether this is already
> possible?

As the regex is just a text string, you could store regexen as text
and just flip your regex expression around:

CREATE TABLE shipping_prices (
shipping_price_id SERIAL PRIMARY KEY
, shipping_price NUMERIC NOT NULL
, ship_from_zip TEXT NOT NULL
, ship_to_zip TEXT NOT NULL
, UNIQUE (ship_from_zip, ship_to_zip));
NOTICE: CREATE TABLE will create implicit sequence
"shipping_prices_shipping_price_id_seq" for serial column
"shipping_prices.shipping_price_id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"shipping_prices_pkey" for table "shipping_prices"
NOTICE: CREATE TABLE / UNIQUE will create implicit index
"shipping_prices_ship_from_zip_key" for table "shipping_prices"
CREATE TABLE
INSERT INTO shipping_prices (shipping_price, ship_from_zip,
ship_to_zip) VALUES (2.00,'23773', '879[0-9]{2,2}');
INSERT INTO shipping_prices (shipping_price, ship_from_zip,
ship_to_zip) VALUES (3.00,'23775', '879[0-9]{2,2}');
INSERT INTO shipping_prices (shipping_price, ship_from_zip,
ship_to_zip) VALUES (1.00,'23773', '877[0-9]{2,2}');
SELECT * FROM shipping_prices;
shipping_price_id | shipping_price | ship_from_zip | ship_to_zip
-------------------+----------------+---------------+---------------
1 | 2.00 | 23773 | 879[0-9]{2,2}
2 | 3.00 | 23775 | 879[0-9]{2,2}
3 | 1.00 | 23773 | 877[0-9]{2,2}
(3 rows)

SELECT shipping_price
FROM shipping_prices
WHERE ship_from_zip = '23773'
AND '87927' ~ ship_to_zip;
shipping_price
----------------
2.00
(1 row)

There may be another way, but I believe this should work, if I
understand you correctly.

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-04-21 16:29:57 Re: Regular Expression Data Type
Previous Message Tom Lane 2007-04-21 16:01:46 Re: Problem compiling PostgreSQL 8.2.3 on RedHat Enterprise Server 2.1?