【问题标题】:Extracting multiple substrings from a single string using regular expressions in Snowflake在雪花中使用正则表达式从单个字符串中提取多个子字符串
【发布时间】:2021-12-08 04:13:34
【问题描述】:

目前,我正在使用Snowflake 中的regular expressions 从单个字符串中提取substring。这就是我现在所拥有的。

例如String = 'These are the Id's which I have WPD4567, WPD36785 also this is another id WPD111234 '

我想提取所有以WPD 开头的substrings,即WPD4567WPD36785WPD111234

我想将这些子字符串放在同一列或不同列中。

这是我目前尝试过的:

select 
   REGEXP_SUBSTR('These are the Id's which I have WPD4567, WPD36785 also this is another id WPD111234 ','(WPD)(.*)\\d*')

实际输出: WPD111234

预期输出: WPD4567 WPD36785 WPD111234

【问题讨论】:

  • 您是否看过一个非常相似的问题:stackoverflow.com/questions/59188198/… 如果您希望将所有值放在一行中,那么 javascript UDF 版本很有意义?目前尚不清楚您在 SQL 行、列上下文中的“一行中的三个值”代表什么。

标签: sql snowflake-cloud-data-platform


【解决方案1】:

我喜欢用 JS UDF 解决这个问题的想法,但这是一个纯 SQL 的解决方案,使用 split_to_table()

with data as (
    select $1 id, $2 val, $3 s
    from values(
        1, 'x', $$These are the Id's which I have WPD4567, WPD36785 also this is another id WPD111234$$
    ),(
        2, 'y', $$WPD10101 These are the Id's which I have WPD4567, WPD36785 also this is another id WPD111234$$
    )
)

select any_value(data.id) id, any_value(data.val) val 
    , listagg('WPD' || regexp_substr(value, '[\\d]*'), ' ') wpds
from data, table(split_to_table(s, 'WPD'))
where index>1
group by seq

【讨论】:

    猜你喜欢
    • 2023-02-09
    • 2010-10-14
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多