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?
select string_agg(x[1],'') from regexp_matches('aaa bbb 888 111122222 1234 bbb ccc ddd','(.)\1*','g') t(x);a b 8 12 1234 b c d, which isn't exactly what the OP wanted, but I upvoted your comment because it is close.select string_agg(x,'') from unnest(array(select regexp_matches('aaa bbb 888 111122222 1234, bbb ccc ddd','\m(\w)\w*(\W*)','g'))) t(x);