PostgreSQL REGEXP_REPLACE括号

时间:2018-04-04 15:22:56

标签: sql postgresql

为什么这个会起作用:

SELECT REGEXP_REPLACE(REGEXP_REPLACE('[GREY]', '\]', ''), '\[', '')

但这些不是:

SELECT REGEXP_REPLACE('[GREY]', '[\[\]]', '')
SELECT REGEXP_REPLACE('[GREY]', '/[\[\]]/g', '')

这是来自regex101.com的解释:

/[\[\]]/g

Match a single character present in the list below [\[\]]
    \[ matches the character [ literally (case sensitive)
    \] matches the character ] literally (case sensitive)

Global pattern flags
    g modifier: global. All matches (don't return after first match)

匹配信息:

Match 1
    Full match  0-1 `[`
Match 2
    Full match  5-6 `]`

1 个答案:

答案 0 :(得分:1)

REGEXP_REPLACE带有标志,例如g作为单独的参数:

SELECT REGEXP_REPLACE('[GREY]', '[\[\]]', '', 'g')返回GREY