P
P
pqgg7nwkd42020-02-09 19:39:55
PostgreSQL
pqgg7nwkd4, 2020-02-09 19:39:55

What is the error of using regexp_replace in postgres 12?

Before I give the essence in the form of examples, please DO NOT stick to the meaning, only to the mechanics of the function itself.

So examples.
How to cut out the tags (do not stick to the point, please):

SELECT regexp_replace('foo <a>123</a> bar <a>123</a> baz', '<a>.*?</a>', '', 'g');
-- foo  bar  baz

Everything is ok, as expected.

Let's complicate things a bit:
SELECT regexp_replace('foo <a X>123</a> bar <a Y>123</a> baz', '<a [XY]>.*?</a>', '', 'g');
-- foo  bar  baz

Everything is ok, as expected.

Change the square brackets to round ones:
SELECT regexp_replace('foo <a X>123</a> bar <a Y>123</a> baz', '<a (X|Y)>.*?</a>', '', 'g');
-- foo  baz

Why did bar eat up?

Postgres version: 12. Function documentation here: https://postgrespro.ru/docs/postgrespro/12/functio...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pqgg7nwkd4, 2020-02-09
@pqgg7nwkd4

The answer is found in IRC postgres and documentation.
You need to write like this:

SELECT regexp_replace('foo <a X>123</a> bar <a Y>123</a> baz', '<a (X|Y){1,1}?>.*?</a>', '', 'g');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question