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

在Snowflake/SQL中用正则提取Ruby序列化哈希指定字段值的方法

在Snowflake/SQL中提取Ruby序列化哈希的指定字段内容

针对你提供的Ruby序列化哈希(YAML格式),要提取host_goal_to_have和host_goal_to_be_able后的内容,关键是处理内容可能跨多行的情况——这类内容的结束标记是下一个非缩进的键(即开头无空格的key:行)或者字符串末尾。

解决方案

使用Snowflake的REGEXP_SUBSTR函数,结合单行模式正则表达式匹配跨多行内容:

SELECT
  -- 提取host_goal_to_have的完整内容(含跨换行的部分)
  REGEXP_SUBSTR(your_ruby_hash_column, 'host_goal_to_have:\\s*(.*?)(?=\\n\\S|$)', 1, 1, 's') AS extracted_host_goal_to_have,
  -- 提取host_goal_to_be_able的内容
  REGEXP_SUBSTR(your_ruby_hash_column, 'host_goal_to_be_able:\\s*(.*?)(?=\\n\\S|$)', 1, 1, 's') AS extracted_host_goal_to_be_able
FROM your_target_table;

正则说明

  • host_goal_to_have:\\s*:精准匹配字段名,跳过字段名后的任意空白(包括换行和空格)
  • (.*?):非贪婪模式匹配任意字符,搭配s模式(单行模式)让.能识别换行符,覆盖跨多行的内容
  • (?=\\n\\S|$):正向预查,匹配到下一行以非空白字符开头(即下一个哈希键)或字符串末尾时停止,确保只提取目标字段内容

测试验证

用你提供的样本数据测试,上述SQL会输出:

  • extracted_host_goal_to_have:established a thriving community of actors supporting each other and turning passion into income!
  • extracted_host_goal_to_be_able:reach people and unlock the artist inside them

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 06:06:45