如何在Redshift(或替代方法)中使用正则表达式捕获组 [英] How to use a regex capture group in redshift (or alternative)

查看:388
本文介绍了如何在Redshift(或替代方法)中使用正则表达式捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在redshift列中有一个字段,如下所示:

I have a field in a redshift column that looks like the following:

abcd1234df-TEXT_I-WANT

前10位数字中的字符和数字可以是字母或数字.

the characters and numbers in the first 10 digits can be either letters or numbers.

如果我使用捕获组正则表达式,则会使用写得不好的表达式,例如(\w\w\w\w\w\w\w\w\w\w\W)(.*)并抓取第二组

If I use a capture group regex, I would use a poorly written expression like (\w\w\w\w\w\w\w\w\w\w\W)(.*) and grap the 2nd group

但是我在redshift中实现此功能时遇到了麻烦,因此不确定我如何只能在第一个连字符后抓取内容

But I'm having trouble implementing this in redshift, so not sure how I can grab only the stuff after the first hyphen

推荐答案

如前所述,正则表达式可能会过大.但是,它在某些情况下可能很有用.

As mentioned before, regex might be an overkill. However, it could be useful in some cases.

这是一个基本的替换模式:

SELECT
    regexp_replace(
      'abcd1234df-TEXT_I-WANT'  -- use your input column here instead
    , '^[a-z0-9]{10}-(.*)$'     -- matches whole string, captures "TEXT_I-WANT" in $1
    , '$1'                      -- inserts $1 to return TEXT_I-WANT
    )
;

这篇关于如何在Redshift(或替代方法)中使用正则表达式捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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