pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedRegular Expression in SQL
2+ messages / 2 participants
[nested] [flat]
* Regular Expression in SQL
@ 2006-07-15 22:03 Aaron Bono <postgresql@aranya.com>
2006-07-16 02:22 ` Re: Regular Expression in SQL Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: Aaron Bono @ 2006-07-15 22:03 UTC (permalink / raw)
To: 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
==================================================================
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Regular Expression in SQL
2006-07-15 22:03 Regular Expression in SQL Aaron Bono <postgresql@aranya.com>
@ 2006-07-16 02:22 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2006-07-16 02:22 UTC (permalink / raw)
To: Aaron Bono <postgresql@aranya.com>; +Cc: pgsql-sql
"Aaron Bono" <postgresql@aranya.com> writes:
> 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.
Hmm ... I think that this is a bug in similar_escape(): it ought to
transform parentheses in a SIMILAR pattern into non-capturing parentheses.
Until this is fixed, your best bet is to use the POSIX-regexp form of
substring(). You can't sneak non-capturing parens through
similar_escape, because it'll try to escape the ? ...
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2006-07-16 02:22 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-15 22:03 Regular Expression in SQL Aaron Bono <postgresql@aranya.com>
2006-07-16 02:22 ` Tom Lane <tgl@sss.pgh.pa.us>
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