Regular Expression in SQL

From: "Aaron Bono" <postgresql(at)aranya(dot)com>
To: "Postgres SQL language list" <pgsql-sql(at)postgresql(dot)org>
Subject: Regular Expression in SQL
Date: 2006-07-15 22:03:23
Message-ID: bf05e51c0607151503j53fffb01y38840a72c43a50f6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I recall not long ago a discussion about regular expressions in a query that
hit on this exact topic but don't think it was ever resolved so I am giving
it a go again...

Here is my query (keep in mind that I am just experimenting now so don't
worry about the fact that I am using nested substring function calls):

SELECT
referrer,
substring(referrer FROM '^([^\\/]*\\/\\/[^\\/]*)(\\/)?'),
substring(referrer FROM
'^[^\\/]*\\/\\/www\\.google\\.[^\\/]*\\/[^\\?]*\\?(.*)$'),
substring(substring(referrer FROM
'^[^\\/]*\\/\\/www\\.google\\.[^\\/]*\\/[^\\?]*\\?(.*)$') FROM
'((%&q=)|(q=))#"[^&]*#"((&%)|())' FOR '#')
FROM one_hour_air.web_page_view
WHERE referrer ~ '^[^\\/]*\\/\\/(www.google\\.[^\\/]*)\\/'

What I get is:

referrer
substring
substring_1
substring_2

http://www.google.ca/search?q=one+hour+heating&hl=en
http://www.google.ca
q=one+hour+heating&hl=en
q=

http://www.google.com/search?hl=en&q=One+hour+heating+and+Air
http://www.google.com
hl=en&q=One+hour+heating+and+Air
hl=en&q=

What I expected for substring_2 was (respectively):

one+hour+heating

One+hour+heating+and+Air

I thought by using the FOR '#' I could specify exactly what part of the
expression I would get but it still grabs the first (...) of the pattern.
At least that is what the documentation in seciton 9.7.2 at
http://www.postgresql.org/docs/8.1/static/functions-matching.html led me to
believe. How can I get the part of the string I am really after without
using one nested substring after another?

Thanks,
Aaron Bono

==================================================================
Aaron Bono
Aranya Software Technologies, Inc.
http://www.aranya.com
==================================================================

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Aaron Bono 2006-07-15 22:10:14 Re: Doubt about User-defined function.
Previous Message Paul S 2006-07-15 12:00:42 Re: Querying for name/value pairs in reverse