Thread: Regular Expression Data Type

Regular Expression Data Type

From
Richard Doust
Date:
Hi.
I appreciate that I can query the database and find records that
match a regular expression.
What I'd like to be able to do is to define a field in the database
as a regular expression so that when I select, i.e.,

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?

Thanks.

Re: Regular Expression Data Type

From
Michael Glaesemann
Date:
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



Re: Regular Expression Data Type

From
Tom Lane
Date:
Richard Doust <rdoust@mac.com> writes:
> What I'd like to be able to do is to define a field in the database
> as a regular expression so that when I select, i.e.,

> 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]*'.

I don't see the problem ... you just write a variable instead of a
constant on the right-hand side of ~, no?

How efficient this might be is another question, but it works.

            regards, tom lane