From: Jeff Eckermann <jeckermann@verio.net>
To: pgsql-sql@postgresql.org
Subject: RE: RE: Re: select substr???
Date: Thu, 12 Apr 2001 14:04:54 -0500
Message-ID: <08CD1781F85AD4118E0800A0C9B8580B094A18@NEZU> (raw)
The references to REs in the docs are a little hard to find (maybe it's just
me). Bruce's book gives a good coverage, with the benefit of an index to
find where that is. AFAICT, the implementation is "extended regular
expressions", like egrep.
My choice of characters to match was a little arbitrary: it would depend on
the specification.
You are correct, the check for "$1 is null" is not required. I was
attempting an optimisation, as in "don't do anything else if this is null".
The gain would depend on how much further processing the function would
attempt before recognizing that it was dealing with a null value, which is
something that I don't know enough to tell.
> -----Original Message-----
> From: Albert REINER [SMTP:areiner@tph.tuwien.ac.at]
> Sent: Thursday, April 12, 2001 10:44 AM
> To: pgsql-sql@postgresql.org
> Subject: Re: RE: Re: select substr???
>
> Thanks, interesting. I did not find any mention of regular expressions
> in the 7.0.2 docs I installed locally.
>
> BTW, your code does not do exactly the same, as it removes any
> whitespace while the other one only collapses consecutive blanks. But,
> of course, regular expressions in PL/pgSQL make this much easier.
>
> As a further aside, I think that in both versions of the function the
> check for `$1 IS NULL' is not necessary; I got the impression that
> passing NULL as an argument to a function will automatically return
> NULL as the result, doesn't it?
>
> Albert.
>
>
> On Tue, Apr 10, 2001 at 05:41:26PM -0500, Jeff Eckermann wrote:
> > Regular expressions make this much easier. The below could be shortened
> to:
> >
> > create function ComparisonString(text) returns text as '
> > declare
> > t alias for $1;
> > r text;
> > c char;
> > begin
> > if t is null or t !~ ''[^a-zA-Z0-9]''
> > then
> > return t;
> > end if;
> > r = '''';
> > for i in 1 .. char_length(t) loop
> > c = substring(t from i for 1);
> > if c ~ ''[a-zA-Z0-9]''
> > then
> > r = r || c;
> > end if;
> > end loop;
> > return r;
> > end;
> > ' language 'plpgsql' with (IsCachable);
> >
> > > -----Original Message-----
> > > From: Albert REINER [SMTP:areiner@tph.tuwien.ac.at]
> ...
> > >
> > > somewhere I use the following, which might be adapted to do what you
> > > want. I am sure there are more elegant ways of doing this, though.
> > >
> > > create function ComparisonString(text) returns text as '
> > > declare
> > > t text;
> > > r text;
> > > c char;
> > > ns bool;
> > > begin
> > > if $1 is null then
> > > return NULL;
> > > end if;
> > > t = lower(trim(both $1));
> > > r = '''';
> > > ns = false;
> > > for i in 1 .. char_length(t) loop
> > > c = substring(t from i for 1);
> > > if c = '' '' then
> > > if ns then
> > > r = r || '' '';
> > > end if;
> > > ns = false;
> > > else
> > > if position(c in ''abcdefghijklmnopqrstuvwxyz0123456789'') > 0
> > > then
> > > r = r || c;
> > > ns = true;
> > > end if;
> > > end if;
> > > end loop;
> > > return trim(both r);
> > > end;
> > > ' language 'plpgsql' with (IsCachable);
> > >
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl
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: jeckermann@verio.net
Subject: Re: select substr???
In-Reply-To: <08CD1781F85AD4118E0800A0C9B8580B094A18@NEZU>
References: <08CD1781F85AD4118E0800A0C9B8580B094A18@NEZU>
* 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