Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UKTcQ-0004h2-7r for pgsql-sql@arkaria.postgresql.org; Tue, 26 Mar 2013 13:08:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UKTcP-0000OU-FG for pgsql-sql@arkaria.postgresql.org; Tue, 26 Mar 2013 13:08:49 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UKTcO-0000OP-JV for pgsql-sql@postgresql.org; Tue, 26 Mar 2013 13:08:48 +0000 Received: from mail-pd0-f174.google.com ([209.85.192.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UKTcK-00054p-JQ for pgsql-sql@postgresql.org; Tue, 26 Mar 2013 13:08:47 +0000 Received: by mail-pd0-f174.google.com with SMTP id p12so1066079pdj.5 for ; Tue, 26 Mar 2013 06:08:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:user-agent:date:subject:from:to:message-id:thread-topic :mime-version:content-type:x-gm-message-state; bh=jw5uTYBVKoD8CE4yJwz3Q8srAOfw9fCGlszavqrQunM=; b=KSOggQpWbxdsDfYLg8DfAvwej4kGJ5r1Zr/x6cQrFhmOYYKQ7gPDzCNFbYkIbqBweE bwRD4rLCiY4QR+7HSjp2JhHZymBI+RUyrhef6KigU2N09nvNR+VHmue8/5BkojftHMyI n76MskR0oswcHGJcaZGB4BcZN0diFZkXwYKL5I78qpzOuWZvdE4kuecAWqhVQciK4NcG qQE2DKJ1MhlKoE+pBwY6/F2Am9aeWPM054WoViWUfc7lPjRPn37OR2y3qHe7gk+CV0fy 7Ry2k8m+TmH1e80rryGMbV7TE8gAca3FtYnQbcrt9016qtFptKRoEzAKMbHrADvDyaAD /m5g== X-Received: by 10.66.168.6 with SMTP id zs6mr23783445pab.5.1364303323418; Tue, 26 Mar 2013 06:08:43 -0700 (PDT) Received: from [10.0.1.10] (adsl-074-245-040-156.sip.clt.bellsouth.net. [74.245.40.156]) by mx.google.com with ESMTPS id qd8sm17476845pbc.29.2013.03.26.06.08.39 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 26 Mar 2013 06:08:41 -0700 (PDT) User-Agent: Microsoft-MacOutlook/14.3.2.130206 Date: Tue, 26 Mar 2013 09:08:34 -0400 Subject: how can I replace all instances of a pattern From: James Sharrett To: Message-ID: Thread-Topic: how can I replace all instances of a pattern Mime-version: 1.0 Content-type: multipart/alternative; boundary="B_3447133720_23432106" X-Gm-Message-State: ALoCoQkgEtZsoEBqmyOAKh2qxPa5V84AyMVMPiSRf/2CEG+otlnWV+8f1b5itxwxg7oy0NGex+R3 X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org > This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --B_3447133720_23432106 Content-type: text/plain; charset="ISO-8859-1" Content-transfer-encoding: quoted-printable I'm trying remove all instances of non-alphanumeric or underscore character= s from a query result for further use. This is part of a function I'm writin= g that is in plpgsql Examples: Original value 'My text1' 'My text 2' 'My-text-3' 'My_text4' 'My!text5' Desired 'Mytext1' 'Mytext2' 'Mytext3' 'My_text4' (no change) 'Mytext5' The field containing the text is column_name. I tried the following: Select regexp_replace(column_name,'\W','') from mytable This deals with the correct characters but only does the first instance of the character so the output is: 'My text1' 'Mytext 2' (wrong) 'Mytext-3' (wrong) 'My_text4' 'My!text5' I managed to get the desired output by writing the text into a variable through a loop and then just keep looping on the variable until all the characters are removed: sql_qry:=3D 'select column_name from mytable'; for sql_record in execute sql_qry loop curr_record :=3D sql_record.column_name; while length(substring(curr_record from '\W'))>0 loop curr_record :=3D regexp_replace(curr_record, '\W',''); end loop; =8A. rest of the code This works but it seems like a lot of work to do something this simple but = I cannot find any function that will replace all instances of a string AND ca= n base it on a regular expression pattern. Is there a better way to do this in 9.1? --B_3447133720_23432106 Content-type: text/html; charset="ISO-8859-1" Content-transfer-encoding: quoted-printable
I'm trying remove all instan= ces of non-alphanumeric or underscore characters from a query result for fur= ther use.  This is part of a function I'm writing that is in plpgsql

Examples:

  Original va= lue
    'My text1'
    'My text 2'
    'My-text-3'
    'My_text4'
<= div>    'My!text5'

   Desired
    'Mytext1'
    'Mytext2'
    'Mytext3'
    'My_text4'  (no c= hange)
    'Mytext5'


=
The field containing the text is column_name.  I tried the followi= ng:

  Select regexp_replace(column_name,'\W','= ') from mytable

This deals with the correct charact= ers but only does the first instance of the character so the output is:

    'My text1'
    '= Mytext 2'  (wrong)
    'Mytext-3'  (wrong)
    'My_text4'
    'My!text5'

I managed to get the desired output by writing the tex= t into a variable through a loop and then just keep looping on the variable = until all the characters are removed:

sql_qry:=3D 'se= lect column_name from mytable';

for sql_record= in execute sql_qry loop
curr_record :=3D sql_record.column_name;

        while length(substring(curr_record from '\W'))>0 lo= op
            curr_record :=3D regexp_replace(curr_= record, '\W','');
        end loop;

…. rest of the code

This works but it = seems like a lot of work to do something this simple but I cannot find any f= unction that will replace all instances of a string AND can base it on a reg= ular expression pattern.  Is there a better way to do this in 9.1? --B_3447133720_23432106--