pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Aaron Bono <postgresql@aranya.com>
To: Postgres SQL language list <pgsql-sql@postgresql.org>
Subject: Regular Expression in SQL
Date: Sat, 15 Jul 2006 17:03:23 -0500
Message-ID: <bf05e51c0607151503j53fffb01y38840a72c43a50f6@mail.gmail.com> (raw)

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
==================================================================

view thread (2+ messages)  latest in thread

Message-ID: <bf05e51c0607151503j53fffb01y38840a72c43a50f6@mail.gmail.com>
Permalink:  ../bf05e51c0607151503j53fffb01y38840a72c43a50f6@mail.gmail.com/
Also on:    postgresql.org/message-id/bf05e51c0607151503j53fffb01y38840a72c43a50f6@mail.gmail.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: postgresql@aranya.com
  Subject: Re: Regular Expression in SQL
  In-Reply-To: <bf05e51c0607151503j53fffb01y38840a72c43a50f6@mail.gmail.com>

* 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