如何在PostgreSQL中替换表列值中的特定字符串

时间:2019-03-08 09:39:49

标签: postgresql pattern-matching

我正在尝试用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

1 个答案:

答案 0 :(得分:1)

这可能是一个起点:

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/([^"]*)';