You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Snowflake中如何提取关键字'theTags'后的首组字母数字字符

Snowflake中提取'theTags'后的第一组字母数字字符

问题场景

现有Snowflake表sample,列v包含如下数据:

create table sample as
select ' dsaljkdlsakj 232 11 sdlksaffeoi theTags: 123456 , wewds l [ ] ' as v
union 
select 'weqreqf fdffrok skdjsa theTags 1233228, dsdad 22 ' as v
union 
select ' eeofkdspof kosadk 3e theTags: ab123232 & [dsda ] '  as v
union
select ' eeofkdspof kosadk 3e theTags23248ab & [dsda ] '  as v
union
select ' eeofkdspof kosadk 3e theTags 23233ab# & [dsda ] '  as v

需要提取关键字theTags之后的第一组连续字母数字字符,要求排除后续的非字母数字符号(比如#、逗号等)。

错误方法分析

  • 第一种写法仅返回theTags本身,因为正则只匹配了关键字,未涉及后续目标字符:
    select reg_exp_substr(v,'theTags',1,1,'e') AS result from sample
    
  • 第二种写法硬编码了字符长度,且依赖空格分隔,无法处理theTags直接拼接目标字符(如theTags23248ab)或带冒号的场景,同时不能自动终止在非字母数字字符前:
    select 
    SPLIT_PART(reg_exp_substr(v,'theTags [a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]'), ' ',2) as result 
    from sample
    

正确解决方案

使用带捕获组的正则表达式,通过REGEXP_SUBSTR直接提取目标内容:

SELECT 
  REGEXP_SUBSTR(v, 'theTags[^a-zA-Z0-9]*([a-zA-Z0-9]+)', 1, 1, 'e', 1) AS result
FROM sample;

正则说明

  • theTags:精准匹配关键字
  • [^a-zA-Z0-9]*:匹配关键字后所有非字母数字的分隔字符(包括冒号、空格,甚至无分隔直接拼接的情况,*表示0个或多个)
  • ([a-zA-Z0-9]+):捕获第一组连续的字母数字字符,+表示至少一个,遇到第一个非字母数字字符(如#、逗号)自动停止
  • 最后一个参数1:指定返回正则表达式中第一个捕获组的内容

执行结果

result
123456
1233228
ab123232
23248ab
23233ab

内容的提问来源于stack exchange,提问作者StackOne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:23:13