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 C01919FA612 for ; Fri, 7 Jul 2006 17:49:28 -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 80167-03 for ; Fri, 7 Jul 2006 17:49:23 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from dougal.cranntara.net (unknown [217.112.91.201]) by postgresql.org (Postfix) with ESMTP id 95A209FA603 for ; Fri, 7 Jul 2006 17:49:23 -0300 (ADT) Received: from host217-32-120-150.webport.bt.net ([217.32.120.150] helo=bolek.coachhouse) by dougal.cranntara.net with asmtp (Exim 3.36 #1 (Debian)) id 1FyxHh-0003ja-00 for ; Fri, 07 Jul 2006 21:50:46 +0100 Received: (Exim 3.36) #1 (Debian)) protocol: esmtp id 1FyxGA-0005KO-00 ; Fri, 07 Jul 2006 21:49:10 +0100 Message-ID: <44AEC8C5.80103@numerixtechnology.de> Date: Fri, 07 Jul 2006 21:49:09 +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> <44AEAED9.7080300@myemma.com> In-Reply-To: <44AEAED9.7080300@myemma.com> 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/119 X-Sequence-Number: 25693 Erik Jones wrote: > 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 >> 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 >> > Will the mm always be the end of the base name? I had thought it best to simplify the problem for the purposes of the mailing list but maybe I should supply the complete problem and describe the purpose of the exercise: I am trying to come up with a semi-automatic solution to tidy up some data. If it's got to be done manually via the GUI it would mean a lot of dummy work [for the customer]. First of all I did a 5 table join to select those NAMEs which don't follow the required pattern: the pattern describes a photographic lens (focal length followed by lens speed (aperture)) and nothing else. Unfortuantely, there are a few hundred occurences where a few attributes have been appended which should have been stored elsewhere. valid entries would be: "28mm F2.8" (prime lens) "30-70mm F4" (zoom lens) "30-70mm F2.8" (zoom lens) "30-100mm F4.5-5.6" (zoom lens with variable speed) In the WHERE clause I have specified all those NAMEs, which follow that pattern but have some gubbins appended: WHERE NAME ~ '^((\\d+(-\\d+)?)mm F((\\d+(\.\\d+)?)+(\.(\\d+(\.\\d+)?)+)?))\\D+$' which gives me a listing of those candidates that need to be amended - manually or otherwise. Next, I wanted to produce a result which splits NAME into what it should be (BASE) and attributes (SUFFIX). Maybe I can generate some SQL from that to tidy up the data. -- Regards, Tarlika Elisabeth Schmitz