Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

444
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!