pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Reverse pattern match.
4+ messages / 3 participants
[nested] [flat]

* Reverse pattern match.
@ 2003-08-18 07:05  Moonstruck <bogus.email@pls-relpy-to.group>
  0 siblings, 2 replies; 4+ messages in thread

From: Moonstruck @ 2003-08-18 07:05 UTC (permalink / raw)
  To: pgsql-sql

I want to create a table of  regular expression patterns (for assessing
phone numbers), something like:
CREATE TABLE CallType ( pattern varchar primary key,
                        type    varchar,
                        rate    int4);
INSERT INTO CallType VALUES ('0[3-9]________','Interstate Call',50);
INSERT INTO CallType VALUES ('9_______','Local Call',25);
INSERT INTO CallType VALUES ('0011__________%','International Call',100);

Then determine call types, based on a match, something like:


PhoneNumber := '99116633';
SELECT type, rate FROM CallType where pattern LIKE PhoneNumber;

(Sorry about the pseudo-code), but you get the gist. The query returns a
calltype description and a call rate based on the comparison of the actual
phone-number to a table of RE patterns.

I can't get my head around a way to do this, can anyone help?

Guy






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Reverse pattern match.
@ 2003-08-18 23:05  Rod Taylor <rbt@rbt.ca>
  parent: Moonstruck <bogus.email@pls-relpy-to.group>
  1 sibling, 0 replies; 4+ messages in thread

From: Rod Taylor @ 2003-08-18 23:05 UTC (permalink / raw)
  To: Moonstruck <bogus.email@pls-relpy-to.group>; +Cc: pgsql-sql

On Mon, 2003-08-18 at 03:05, Moonstruck wrote:
> I want to create a table of  regular expression patterns (for assessing
> phone numbers), something like:
> CREATE TABLE CallType ( pattern varchar primary key,
>                         type    varchar,
>                         rate    int4);
> INSERT INTO CallType VALUES ('0[3-9]________','Interstate Call',50);
> INSERT INTO CallType VALUES ('9_______','Local Call',25);
> INSERT INTO CallType VALUES ('0011__________%','International Call',100);
> 
> Then determine call types, based on a match, something like:
> 
> 
> PhoneNumber := '99116633';
> SELECT type, rate FROM CallType where pattern LIKE PhoneNumber;

SELECT type, rate FROM CallType WHERE PhoneNumber ~ pattern;

Attachments:

  signature.asc (application/pgp-signature, 186B) download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Reverse pattern match.
@ 2003-08-18 23:11  Josh Berkus <josh@agliodbs.com>
  parent: Moonstruck <bogus.email@pls-relpy-to.group>
  1 sibling, 1 reply; 4+ messages in thread

From: Josh Berkus @ 2003-08-18 23:11 UTC (permalink / raw)
  To: Moonstruck <bogus.email@pls-relpy-to.group>; pgsql-sql

Moonstruck,

> I want to create a table of  regular expression patterns (for assessing
> phone numbers), something like:
> CREATE TABLE CallType ( pattern varchar primary key,
>                         type    varchar,
>                         rate    int4);
> INSERT INTO CallType VALUES ('0[3-9]________','Interstate Call',50);
> INSERT INTO CallType VALUES ('9_______','Local Call',25);
> INSERT INTO CallType VALUES ('0011__________%','International Call',100);

PostgreSQL supports real Regular Expressions, via the ~ operator.   See "~" 
under "functions and operators", and then consult your favorite book or 
online ressouce on how to compose regexps. 

An example of "is not in 415 area code" would be
phone_no ~ '^415\d{7}' 
which should be "415" at the beginning followed by at least 7 other digits.

(Folks, please correct my regex code if it's bad!)

The disadvantage to this approach is that it cannot be indexed.  

-- 
-Josh Berkus
 Aglio Database Solutions
 San Francisco




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Reverse pattern match.
@ 2003-08-20 06:42  Moonstruck <bogus.email@pls-relpy-to.group>
  parent: Josh Berkus <josh@agliodbs.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Moonstruck @ 2003-08-20 06:42 UTC (permalink / raw)
  To: pgsql-sql

Many thanks... got it now.

Example...

sox=# select * from regexpatt;
 pattern |            description
---------+------------------------------------
 ^b      | starts with a B
 ^a      | starts with an A
 ^c.*l$   | starts with a C and ends with an L
(3 rows)

sox=# select description from regexpatt where 'bravo' ~ pattern;
  description
----------------
 starts with a B
(1 row)

sox=# select description from regexpatt where 'caramel' ~ pattern;
            description
------------------------------------
 starts with a C and ends with an L
(1 row)


"Josh Berkus" <josh@agliodbs.com> wrote in message
news:200308181611.27937.josh@agliodbs.com...
Moonstruck,

> I want to create a table of  regular expression patterns (for assessing
> phone numbers), something like:
> CREATE TABLE CallType ( pattern varchar primary key,
>                         type    varchar,
>                         rate    int4);
> INSERT INTO CallType VALUES ('0[3-9]________','Interstate Call',50);
> INSERT INTO CallType VALUES ('9_______','Local Call',25);
> INSERT INTO CallType VALUES ('0011__________%','International Call',100);

PostgreSQL supports real Regular Expressions, via the ~ operator.   See "~"
under "functions and operators", and then consult your favorite book or
online ressouce on how to compose regexps.

An example of "is not in 415 area code" would be
phone_no ~ '^415\d{7}'
which should be "415" at the beginning followed by at least 7 other digits.

(Folks, please correct my regex code if it's bad!)

The disadvantage to this approach is that it cannot be indexed.

--
-Josh Berkus
 Aglio Database Solutions
 San Francisco


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faqs/FAQ.html






^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-08-20 06:42 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-08-18 07:05 Reverse pattern match. Moonstruck <bogus.email@pls-relpy-to.group>
2003-08-18 23:05 ` Rod Taylor <rbt@rbt.ca>
2003-08-18 23:11 ` Josh Berkus <josh@agliodbs.com>
2003-08-20 06:42   ` Moonstruck <bogus.email@pls-relpy-to.group>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox