[英]How to replace particular string in table column value in PostgreSQL
我試圖用PostgreSQL中的其他文本替換某些文本。
更具體地說,我正在嘗試從相對路徑到絕對路徑替換圖像路徑並在article
(表blog_posts
)中錨定href 。 一些圖像和錨點已經具有絕對路徑,不應受到干擾。
我試圖選擇需要修改的記錄:
SELECT
bp.id,
bp.article,
FROM
blog_posts bp
WHERE
bp.article LIKE '%src=%"/fixed_word/%'
OR
bp.article LIKE '%src="/fixed_word/%'
OR
bp.article LIKE '%href="/fixed_word/%'
OR
bp.article LIKE '%href=%"/fixed_word/%'
現在我不確定如何繼續進行更新。 請幫助獲得正確的解決方案。
我的數據是這樣的:
MyTable : blog_posts
id article
1 any text <img any-atribute src="/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="/fixed_word/variable_word2/something2.png"><img src="/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src=\"/fixed_word/variable_word1/something.png"/> any text
5 any text <a any-attribute href=\"/fixed_word/variable_word2/something2.png"><img src=\"/fixed_word/variable_word2/something2.png"/> </a>any text
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
輸出應為:
id article
1 any text <img any-atribute src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
2 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"><img src="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png"/> </a>any text
3 any text <img src="https://mydomain.subdomain.com/fixed_word/variable_word1/something.png"/> any text
4 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_wordvariable_word1/something.png"/> any text
5 any text <a any-attribute href="https://mydomain.subdomain.com/fixed_word/variable_word2/something2.png">
6 any text <img any-attribute src="https://mydomain.subdomain.com/fixed_word/variable_word6/something6.png"/> any text
這可以是一個起點:
UPDATE blog_posts
SET article = regexp_replace(
article,
E'(src|href)=[^"]*"/fixed_word/([^"]*)',
E'\\1="https://mydomain.subdomain.com/fixed_word/\\2',
'g'
)
WHERE article ~ E'(src|href)=[^"]*"/fixed_word/([^"]*)';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.