Removing consecutive characters with the Posgresql regexp_replace function

Remove all duplicate repeating characters using a regular expression.

In Javascript, this works well:

txt='aaa bbb 888 bbb ccc ddd'.replace(/(?!(?!(.)\1))./g,'');

Returns 'a b 8 b c d'

How to do this using the Posgeresql regexp_replace function? This will not work:

SELECT regexp_replace('aaa bbb 888 bbb ccc ddd',E'(?!(?!(.)\\\\1)).','g');

$ psql -c "SELECT regexp_replace('aaa bbb 888 bbb ccc ddd',E'(?!(?!(.)\\1)).','g');"
     regexp_replace      
-------------------------
 aaa bbb 888 bbb ccc ddd
(1 row)

$ psql -c "SELECT regexp_replace('aaa bbb 888 bbb ccc ddd','(?!(?!(.)\1)).','g');"   
ERROR:  invalid regular expression: invalid backreference number

What am I doing wrong?

+4
source share
1 answer

There's a similar SO question to help you get the answer:

SELECT regexp_replace('aaa bbb 888 bbb ccc ddd', '(.)\1{1,}', '\1', 'g');
 regexp_replace 
----------------
 a b 8 b c d
(1 row)

It uses backreference to capture groups of repeated characters.

+1
source

Source: https://habr.com/ru/post/1651352/


All Articles