pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gene Selkov Jr. <selkovjr@mcs.anl.gov>
To: Tony.Curtis@vcpc.univie.ac.at
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] [SQL, regex, words] how to match word boundaries using regex's?
Date: Mon, 19 Oct 1998 09:33:23 -0400
Message-ID: <199810191432.JAA07847@antares.mcs.anl.gov> (raw)
In-Reply-To: <199810191302.PAA05430@haze.vcpc.univie.ac.at>

> I want to do a regex match limited to words.
>
> I tried this:
>
>   where ... ~ '\Wword\W';
>   where ... ~ '\W*word\W*';
>   where ... ~ '\b\(word\)\b';
>
> and other things with LIKE but no joy.

Based on the comments in the source, regexp stuff used in postgres is something like this: http://tiger8.com/us/regexp-manpage.html

I guess there are no backslash macros is POSIX expressions. No joy. By the way, I am wondering what determined the choice of the regexp machine for postgres? Is it performance-related? Is it possible to have the same stuff as in perl?

As to your question, how about a poor man's Altavista like this:

Split the text into words before loading into a special index table. Words are numbered sequentially, so you can search for "phrases": 

Table    = word
+----------------------------------+----------------------------------+-------+
|              Field               |              Type                | Length|
+----------------------------------+----------------------------------+-------+
| rec                              | char()                           |    12 |
| seq                              | int4                             |     4 |
| word                             | text                             |   var |
+----------------------------------+----------------------------------+-------+

SELECT DISTINCT w1.rec 
FROM word w1, word w2 
WHERE 
      w1.word ~ '^a$' 
  AND w2.word ~ '^phrase$' 
  AND w1.rec = w2.rec 
  AND w2.seq - w1.seq = 1; -- Distance between the words

This way, you can control what represents the concept of a 'word' by an external program (perl script, etc.)

Certainly, this method will show suboptimal performance with extra large tables and more than three or four words in a seach phrase. But it is possible to optimise by delegating set operations (joins) and position arithmetic to the client. It works very well for my ~500k tables and the most common queries.

--Gene



view thread (3+ messages)  latest in thread

Message-ID: <199810191432.JAA07847@antares.mcs.anl.gov>
Permalink:  ../199810191432.JAA07847@antares.mcs.anl.gov/
Also on:    postgresql.org/message-id/199810191432.JAA07847@antares.mcs.anl.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: selkovjr@mcs.anl.gov, Tony.Curtis@vcpc.univie.ac.at
  Subject: Re: [SQL] [SQL, regex, words] how to match word boundaries using regex's?
  In-Reply-To: <199810191432.JAA07847@antares.mcs.anl.gov>
  References: <199810191302.PAA05430@haze.vcpc.univie.ac.at> <199810191432.JAA07847@antares.mcs.anl.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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