Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

448
Visualizações
Postgres regexp_replace, using function on 3rd parameter

Using Postgres 9.4, is it possible to apply a function on the captured match?

Example: upper case only the string which are surrounded by double-quotes.

SELECT regexp_replace(
  '123, "name", ignored~me, "Beer & Cheese", pi=3.14',
  '"(.+?)"', 
  upper('"\1"'),
  'g'
);

--Result  : '123, "name", ignored~me, "Beer & Cheese", pi=3.14'
--Expected: '123, "NAME", ignored~me, "BEER & CHEESE", pi=3.14'

It looks like the function appearing in the 3rd parameter of regexp_replace is ignored. Is there anyway to use a function and passing the \n group capture as input argument?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I once had same question, and this

I did not get the answer, but general impression that you are not supposed to use SQL functions on arguments of regular expression functions...

over 4 years ago · Santiago Trujillo Relatório

0

In SQL, functions aren't first-class. They can't be passed. There is no part of the language that's interpreted either. It's declarative, compiled (with an optimizer), and evaluated strictly.

Keep in mind, under normal circumstance an update to any column requires rewriting the whole row (not just the column).

You can still accomplish this I would just be looking to a slightly smarter tool for it..

  • PL/Perl
  • PL/v8

Here is an example with plperl,

CREATE LANGUAGE plperl;

CREATE OR REPLACE FUNCTION perl_dynamic_eval_regexp_replace( IN str text, IN pattern text, IN replacementPerl text, OUT text )
STRICT
AS $BODY$
    my ($input, $pattern, $replacement) = @_;
    $input =~ s/$pattern/eval $replacement/ge;
    return $input;
$BODY$
LANGUAGE plperl
VOLATILE;


SELECT perl_dynamic_eval_regexp_replace(
  '123, "name", ignored~me, "Beer & Cheese", pi=3.14',
  '"(.+?)"', 
  $$uc("$1")$$
);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda