X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 8ECA19FB1E8 for ; Fri, 7 Jul 2006 16:27:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 92793-01 for ; Fri, 7 Jul 2006 16:27:00 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 11A8B9FB1D7 for ; Fri, 7 Jul 2006 16:27:00 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from dougal.cranntara.net (unknown [217.112.91.201]) by svr4.postgresql.org (Postfix) with ESMTP id 4BA415AF091 for ; Fri, 7 Jul 2006 19:26:59 +0000 (GMT) Received: from host217-32-130-245.webport.bt.net ([217.32.130.245] helo=bolek.coachhouse) by dougal.cranntara.net with asmtp (Exim 3.36 #1 (Debian)) id 1Fyvzv-0003gd-00 for ; Fri, 07 Jul 2006 20:28:20 +0100 Received: (Exim 3.36) #1 (Debian)) protocol: esmtp id 1Fyvvb-0005FZ-00 ; Fri, 07 Jul 2006 20:23:51 +0100 Message-ID: <44AEB4C6.9000903@numerixtechnology.de> Date: Fri, 07 Jul 2006 20:23:50 +0100 From: T E Schmitz Reply-To: mailreg@numerixtechnology.de User-Agent: Debian Thunderbird 1.0.7 (X11/20051017) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: SELECT substring with regex References: <44AE66E0.1040606@numerixtechnology.de> <200607071659.14940.gary.stainburn@ringways.co.uk> <44AE9B4F.1020401@numerixtechnology.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/114 X-Sequence-Number: 25688 Rodrigo De Leon wrote: > On 7/7/06, T E Schmitz wrote: > >> But that takes me to the next problem: >> >> For the sake of the example I simplified the regular pattern. >> In reality, BASE_NAME might be: >> >> 28mm >> 28-70mm >> >> So the reg. expr. requires brackets: >> >> substring (NAME, '^(\\d+(-\\d+)?mm)' ) as BASE_NAME >> >> Actually, the pattern is more complex than that and I cannot see how I >> can express it without brackets. > > > Maybe: > > select > substring ('28-70mm LD AD Asp XR Macro', '^[\\d-]*mm' ) as BASE_NAME Sorry, but that would also capture something like 10-30-59mm The pattern describes either a single length (120 millimeters) or a range (30 to 70 millimetres), hence: \\d+(-\\d+)?mm The ? quantifier refers to the combination of '-' and digits and has to be bracketed. If the brackets cannot be avoided in the expression, your original suggestion might come in handy though: SELECT substring (NAME, '^\\d+(-\\d+)?mm') AS BASE_NAME , substr( NAME , char_length( substring (NAME, '^\\d+(-\\d+)?mm') ) + 2 ) AS SUFFIX Still, I'd be interested to know whether there is a 'more elegant' solution. -- Regards, Tarlika Elisabeth Schmitz