Skip to content

Instantly share code, notes, and snippets.

@simonewebdesign
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save simonewebdesign/f145078ad157ba5b36c8 to your computer and use it in GitHub Desktop.

Select an option

Save simonewebdesign/f145078ad157ba5b36c8 to your computer and use it in GitHub Desktop.
PostgreSQL regexp_replace example
CREATE OR REPLACE FUNCTION isFoo(text) RETURNS BOOLEAN AS '
SELECT $1 ~* ''insert_regex_here'' AS RESULT
' LANGUAGE SQL;
UPDATE blocks
SET content=(SELECT regexp_replace(content, 'insert_regex_here', 'insert_replace_text_here'));
;--WHERE isFoo(content);
SELECT *
FROM blocks
WHERE isFoo(content);
@simonewebdesign
Copy link
Copy Markdown
Author

The UPDATE should replace every occurrence of URLs like //../../../foo/whatever with a forward slash.

We'll have to execute that SQL query at least 4 times because it's actually replacing only the first occurrence of the regex match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment