MB34
MB34

Reputation: 4404

PostgreSQL REGEXP_REPLACE Brackets

Why would this one work:

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

But these not:

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

This is the explanation from 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)

And Match Information:

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

Upvotes: 0

Views: 2491

Answers (1)

404
404

Reputation: 8572

REGEXP_REPLACE takes flags e.g. g as a separate parameter:

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

Upvotes: 3

Related Questions