postgresql - replace all instances of a string within text field

后端 未结 4 2209
时光取名叫无心
时光取名叫无心 2020-11-28 18:57

In postgresql, how do I replace all instances of a string within a database column?

Say I want to replace all instances of cat with dog, fo

4条回答
  •  情书的邮戳
    2020-11-28 19:41

    The Regular Expression Way

    If you need stricter replacement matching, PostgreSQL's regexp_replace function can match using POSIX regular expression patterns. It has the syntax regexp_replace(source, pattern, replacement [, flags ]).

    I will use flags i and g for case-insensitive and global matching, respectively. I will also use \m and \M to match the beginning and the end of a word, respectively.

    There are usually quite a few gotchas when performing regex replacment. Let's see how easy it is to replace a cat with a dog.

    SELECT regexp_replace('Cat bobcat cat cats catfish', 'cat', 'dog');
    -->                    Cat bobdog cat cats catfish
    
    SELECT regexp_replace('Cat bobcat cat cats catfish', 'cat', 'dog', 'i');
    -->                    dog bobcat cat cats catfish
    
    SELECT regexp_replace('Cat bobcat cat cats catfish', 'cat', 'dog', 'g');
    -->                    Cat bobdog dog dogs dogfish
    
    SELECT regexp_replace('Cat bobcat cat cats catfish', 'cat', 'dog', 'gi');
    -->                    dog bobdog dog dogs dogfish
    
    SELECT regexp_replace('Cat bobcat cat cats catfish', '\mcat', 'dog', 'gi');
    -->                    dog bobcat dog dogs dogfish
    
    SELECT regexp_replace('Cat bobcat cat cats catfish', 'cat\M', 'dog', 'gi');
    -->                    dog bobdog dog cats catfish
    
    SELECT regexp_replace('Cat bobcat cat cats catfish', '\mcat\M', 'dog', 'gi');
    -->                    dog bobcat dog cats catfish
    
    SELECT regexp_replace('Cat bobcat cat cats catfish', '\mcat(s?)\M', 'dog\1', 'gi');
    -->                    dog bobcat dog dogs catfish
    

    Even after all of that, there is at least one unresolved condition. For example, sentences that begin with "Cat" will be replaced with lower-case "dog" which break sentence capitalization.

    Check out the current PostgreSQL pattern matching docs for all the details.

    Update entire column with replacement text

    Given my examples, maybe the safest option would be:

    UPDATE table SET field = regexp_replace(field, '\mcat\M', 'dog', 'gi');
    

提交回复
热议问题