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 B32DA9FB261 for ; Fri, 7 Jul 2006 15:58:39 -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 52120-08 for ; Fri, 7 Jul 2006 15:58:34 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from myemma.com (db1.e2ma.net [66.179.147.163]) by postgresql.org (Postfix) with ESMTP id 764A79FB1D4 for ; Fri, 7 Jul 2006 15:58:33 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id 5AAD123CCD; Fri, 7 Jul 2006 14:40:30 -0500 (CDT) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 17580-10; Fri, 7 Jul 2006 14:40:30 -0500 (CDT) Received: from [192.168.1.68] (65.107.156.170.ptr.us.xo.net [65.107.156.170]) by myemma.com (Postfix) with ESMTP id 10DD923CCA; Fri, 7 Jul 2006 14:40:29 -0500 (CDT) Message-ID: <44AEAED9.7080300@myemma.com> Date: Fri, 07 Jul 2006 13:58:33 -0500 From: Erik Jones User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 To: mailreg@numerixtechnology.de Cc: 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: <44AE9B4F.1020401@numerixtechnology.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at myemma.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/112 X-Sequence-Number: 25686 T E Schmitz wrote: > Gary Stainburn wrote: >> On Friday 07 July 2006 14:51, T E Schmitz wrote: >> >>> I would like to split the contents of a column using substring with a >>> regular expression: >>> >>> The column contains something like >>> "150mm LD AD Asp XR Macro" >>> I want to split this into >>> "150mm", "LD AD Asp XR Macro" >>> >> >> >> select substring('150mm LD AD Asp XR Macro','^\\d+mm') as BASE_NAME, >> substring('150mm LD AD Asp XR Macro','^\\d+mm (.*)$') as SUFFIX; >> base_name | suffix >> -----------+-------------------- >> 150mm | LD AD Asp XR Macro >> (1 row) >> >> The brackets surround the required match > > > This is ingenious! I had been looking at chapter 9.6 Pattern Matching. > Am I missing something? I did not realize that the brackets indicate > the required match. > > 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. > > Will the mm always be the end of the base name? substring(NAME, '^(.*?mm)') as BASE_NAME That should match all the way up to the first mm. If there are actually a set number of different units that it might end in, such as mm and cm, you can do: substring(NAME, '^(.*?(mm|cm))') as BASE_NAME That will match everything up to the first mm or cm. Note that you don't have to worry about the second set of brackets returning anything as the regexp version of substring only returns what is matched by the first parenthesised subexpression. -- erik jones software development emma(r)