Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Consecutive Repeated Characters with PostgreSQL regexp_replace function

Remove all consecutive repeated characters using 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 can I do it with Posgresql regexp_replace function? This won't 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?

like image 402
IanS Avatar asked Aug 16 '16 04:08

IanS


1 Answers

There's a similar SO question that can help you to 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 a backreference to capture the groups of repeating characters.

like image 192
mgamba Avatar answered Sep 30 '22 17:09

mgamba