首页 > 解决方案 > SQL - 在正则表达式之后获取第三个字符串

问题描述

所以例如我有这个数据:

rmchat://room/kotak.com/Kotak_Debt_Brokers

我只需要从字符串中获取“Kotak”并将其显示在另一列中。有没有办法得到它?我一直在网上搜索,我似乎无法找到解决方案。请帮我。

STR, REGEXP_SUBSTR(STR, '(.*?)(\/||$)', 1, 2, NULL, 1)

这是我当前的代码,但它输出的最后一个字符串是 Kotak_Debt_Brokers。

标签: sqlregexdatabasestringsnowflake-cloud-data-platform

解决方案


利用

REGEXP_SUBSTR(STR, '.*/([^_]+)', 1, 1, NULL, 1)

或者,或者,

REGEXP_REPLACE(STR, '.*/([^_]+).*', '\\1')

请参阅正则表达式证明/正则表达式证明 ( regexp_replace)

解释

--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^_]+                    any character except: '_' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))

推荐阅读