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

235
Views
Can't fit regexp for a substring

I have to find and remove a substring from the text using regexp in PostgreSQL. The substring corresponds to the condition: <any text between double-quotes containing for|while inside>

Example

Text:

PERFORM xxkkcsort.f_write_log("INFO", "xxkkcsort.f_load_tables__outof_order_system", "   Script for data loading: ", false, v_sql, 0);

So, my purpose is to find and remove the substring "Script for data loading: ". When I tried to use the script below:

SELECT regexp_replace(
   'PERFORM xxkkcsort.f_write_log("INFO", "xxkkcsort.f_load_tables__outof_order_system", "> Table for loading: "||cc.source_table_name , false, null::text, 0);'
 , '(\")(.*(for|while)(\s).*)(\")'
 , '');

I have all the texts inside double-quotes replaced. The result looks like:

PERFORM xxkkcsort.f_write_log(||cc.source_table_name , false, null::text, 0);

What's a proper regular expression to solve the issue?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

any text between double-quotes containing for|while inside

SELECT regexp_replace(string, '"[^"]*\m(?:for|while)\M[^"]*"', '');

" ... literal " (no special meaning here, so no need to escape it)

[^"]* ... character class including all characters except ", 0-n times

\m ... beginning of a word

(?:for|while) ... two branches in non-capturing parentheses
(regexp_replace() works with simple capturing parentheses, too, but it's cheaper this way since you don't use the captured substring. But try either with the replacement '\1', where it makes a difference ...)

\M ... end of a word

[^"]* ... like above

" ... like above

I dropped \s from your expression, as the task description does not strictly require a white-space character (end of string or punctuation delimiting the word ...).

Related:

  • Escape function for regular expression or LIKE patterns
over 4 years ago · Santiago Trujillo Report

0

You canuse

SELECT regexp_replace(
   'PERFORM xxkkcsort.f_write_log("INFO", "xxkkcsort.f_load_tables__outof_order_system", "> Table for loading: "||cc.source_table_name , false, null::text, 0);',
   '"[^"]*(for|while)\s[^"]*"',
   '') AS Result;

Output:

PERFORM xxkkcsort.f_write_log("INFO", "xxkkcsort.f_load_tables__outof_order_system", ||cc.source_table_name , false, null::text, 0);

See the regex demo and the DB fiddle. Details:

  • " - a " char
  • [^"]* - zero or more chars other than "
  • (for|while) - for or while
  • \s - a whitespace
  • [^"]*" - zero or more chars other than " and then a " char.
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!