X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C684B329E92 for ; Thu, 9 Sep 2004 15:02:13 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 45279-04 for ; Thu, 9 Sep 2004 14:02:04 +0000 (GMT) Received: from web20828.mail.yahoo.com (web20828.mail.yahoo.com [216.136.227.167]) by svr1.postgresql.org (Postfix) with SMTP id 62E9C329CFB for ; Thu, 9 Sep 2004 15:02:06 +0100 (BST) Message-ID: <20040909140206.88582.qmail@web20828.mail.yahoo.com> Received: from [24.248.12.141] by web20828.mail.yahoo.com via HTTP; Thu, 09 Sep 2004 07:02:06 PDT Date: Thu, 9 Sep 2004 07:02:06 -0700 (PDT) From: Jeff Eckermann Subject: Re: Isnumeric function? To: Thomas Swan , olly@lfix.co.uk Cc: Josh Berkus , Theo Galanakis , pgsql-sql@postgresql.org In-Reply-To: <413FB8C0.4050008@idigx.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/61 X-Sequence-Number: 18907 Ok, how about this. At least it works in my testing. I have extended it to allow a negative sign (trailing also), which I would expect to be allowed in a comprehensive "isnumeric" function. If I am wrong, feel free to slap me around; although correcting the regex would be more constructive. ;-) create function isnumeric(text) returns boolean as ' select $1 ~ \'(-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)|([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)-?)\' 'language 'sql'; --- Thomas Swan wrote: > Oliver Elphick wrote: > > >On Wed, 2004-09-08 at 18:48, Josh Berkus wrote: > > > > > >>Theo, Oliver, > >> > >> > >> > >>>Any reason why you don't like ~ > '^([0-9]?)+\.?[0-9]*$' ? > >>> > >>> > >>Yes, because it also matches "." , which is not a > valid numeric value. > >> > >> > >> > >>> ~ '^([0-9]+|[0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+)$' > >>> > >>> > >>Ah, the brute force approach ;-) > >> > >> > > > >Nothing like using a nice big hammer! > > > > > > > Would "^([0-9]+\\.{0,1}[0-9]*|\\.[0-9]+)$" be a > little cleaner? > > >>Actually, the above could be written: > >> > >>~ '^([0-9]+)|([0-9]*\\.[0-9]+)$' > >> > >> > > > >But that doesn't allow a trailing decimal point. > > > > > > > >>... though that still seems inelegant to me. Is > there a regex expert in the > >>house? > >> > >> > > > >All the elegant approaches I can think of match the > empty string. There > >must be at least one digit and 0 or 1 decimal point > with no other > >characters permitted. If you use this as a > constraint, you could make > >it elegant and combine it with another constraint > to exclude '' and '.'. > > > > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org > __________________________________ Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail