雪花正则表达式 – 提取定界符之间的字符串

huangapple go评论62阅读模式
英文:

Snowflake Regex - extract string between delimiter

问题

需要帮助在Snowflake中使用正则表达式获取所需的输出。从'-'后的文本一直到';'。

输入字符串为'008-1994; 023-1905; 030-2330'
期望结果是1994;1905;2330

示例2:

输入字符串为'008-XXX; 023-YYY; 030-ZZZ ; 042-AAA'
期望结果是XXX; YYY; ZZZ; AAA

提前感谢。

英文:

Need help in getting desired output in snowflake using RegEx. Text after'-' and up to ';'

Input string '008-1994; 023-1905; 030-2330'
Desired result 1994;1905;2330

Ex2

Input string '008-XXX; 023-YYY; 030-ZZZ ; 042-AAA'
Desired result XXX; YYY; ZZZ; AAA

Thanks in advance.

答案1

得分: 1

这是使用SPLIT的版本

--input string
set str = '008-1994; 023-1905; 030-2330';

select listagg(split_part(C.value::string, '-', 2)) from LATERAL FLATTEN(input=>split($str, ' ')) as c;

输出:

雪花正则表达式 – 提取定界符之间的字符串

英文:

This is the version using a SPLIT

--input string
set str = '008-1994; 023-1905; 030-2330';

select listagg(split_part(C.value::string, '-', 2)) from LATERAL FLATTEN(input=>split($str, ' ')) as c;

Output:

雪花正则表达式 – 提取定界符之间的字符串

答案2

得分: 0

SET STR = '008-1994; 023-1905; 030-2330';

SELECT
REGEXP_SUBSTR($STR, '\d+-(\d+);\s', 1, 1, 'e', 1) AS FIRST,
REGEXP_SUBSTR($STR, '\d+-(\d+);\s', 1, 2, 'e', 1) AS SECOND,
REGEXP_SUBSTR($STR, '\d+-(\d+)', 1, 3, 'e', 1) AS THIRD;

英文:
SET STR= '008-1994; 023-1905; 030-2330';

SELECT 
REGEXP_SUBSTR($STR,'\\d+-(\\d+);\\s',1,1,'e',1) AS FIRST
,REGEXP_SUBSTR($STR,'\\d+-(\\d+);\\s',1,2,'e',1) AS SECOND
,REGEXP_SUBSTR($STR,'\\d+-(\\d+)',1,3,'e',1) AS THIRD;

huangapple
  • 本文由 发表于 2023年5月25日 12:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定