Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lvLDZ-00014l-Ua for pgsql-general@arkaria.postgresql.org; Mon, 21 Jun 2021 14:48:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lvLDY-0007OG-T7 for pgsql-general@arkaria.postgresql.org; Mon, 21 Jun 2021 14:48:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lvLDY-0007Nx-HM for pgsql-general@lists.postgresql.org; Mon, 21 Jun 2021 14:48:04 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lvLDQ-0001nx-F9 for pgsql-general@postgresql.org; Mon, 21 Jun 2021 14:48:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Jay0eUuTrgsIsnu6xHqpiLCUC2lqsmn0w0d6TJSrSc0=; b=QrDfv906eu03iMFcXdwjHA59ou FMCZwPC4fK+k7Z/4yMX5G+AWaty/4Ab4vahw8XdfW4bQxsaZ5f61wCa4ZEOC5RJJyCOIf1HSIs1fA IcaFwc+/hRumrl2DcwSOxBiQb3NInKziakVFt4KrKCwGBhO7B580SwxKhRf51eg1BD8A=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1lvLDM-0008ST-CP; Mon, 21 Jun 2021 16:47:52 +0200 Date: Mon, 21 Jun 2021 16:47:52 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Oliver Kohll Cc: pgsql-general Subject: Re: replace inside regexp_replace Message-ID: <20210621144752.GA12013@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Jun 21, 2021 at 02:27:22PM +0100, Oliver Kohll wrote: > It half works, i.e. it removes the brackets but doesn't seem to process the > inner replace. It's as if the select were just > select regexp_replace( > 'here is [[my text]] to replace and [[some more]]', > E'\\[\\[(.*?)\\]\\]', > E'\\1', > 'g' > ); > I've a feeling I'm missing something fundamental, any idea what? \1 works only if it's argument to regexp_replace. And regexp_replace can't call any other functions. What you could do is: $ select string_agg(x[1] || replace(x[2], ' ', '_') || x[3], '') from regexp_matches( 'here is [[my text]] to replace and [[some more]] and maybe [[a bit longer]] too', '(.*?)\[\[(.*?)\]\](.*?)', 'g') x; string_agg ───────────────────────────────────────────────────────────────── here is my_text to replace and some_more and maybe a_bit_longer (1 row) Or just use plperl, pl/python, or anything like this. Best regards, depesz