匹配Redshift中的连续数字REGEXP_REPLACE [英] Matching consecutive digits REGEXP_REPLACE in Redshift

查看:302
本文介绍了匹配Redshift中的连续数字REGEXP_REPLACE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Redshift中删除字符串中的连续数字.

I'm trying to remove consecutive numbers from a string in Redshift.

我要从'16,16,16,3,3,4,16,16,'中获取'16,3,4,16,'.

以下构造对我不起作用:

The following construction doesn't work for me:

SELECT regexp_replace('16,16,16,3,3,4,16,16,', '(.+)\1{1,}', '\1');

它返回完全相同的字符串. :(

It's returning exactly the same string. :(

谢谢!

推荐答案

以下是使用Redshift python UDF的答案.

Here is the answer using a Redshift python UDF.

create or replace function dedupstring(InputStr varChar)
  returns varchar
stable
as $$
    OutputStr=''
    PrevStr=''
    first=True
    for part in InputStr.split(','):
        if part <> PrevStr:
            if first:
                OutputStr+=part
            else:
                OutputStr+=','+part
            PrevStr=part
            first=False
    return OutputStr
$$ language plpythonu;

Select dedupstring('16,16,16,3,3,4,16,16,');

这将返回'16,3,4,16'

This returns '16,3,4,16,'

这篇关于匹配Redshift中的连续数字REGEXP_REPLACE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆