Re: Regular Expression Data Type - Mailing list pgsql-general

From Michael Glaesemann
Subject Re: Regular Expression Data Type
Date
Msg-id B42A8EFE-D695-4051-B778-E97B1C34BEA9@seespotcode.net
Whole thread Raw
In response to Regular Expression Data Type  (Richard Doust <rdoust@mac.com>)
List 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



pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Problem compiling PostgreSQL 8.2.3 on RedHat Enterprise Server 2.1?
Next
From: Tom Lane
Date:
Subject: Re: Regular Expression Data Type