【问题标题】:RegEx for retrieving a score number from a JSON用于从 JSON 中检索分数的 RegEx
【发布时间】:2019-04-20 01:26:19
【问题描述】:
我需要在得分后提取浮点数。
{"reason_desc":
{
"score":"0.1",
"numOfIndicatrix":"0",
"indicatrix":[]},
"success":true,
"id":"1555039965661065S427A2DCF5787920"
}
我希望输出 0.1 或任何用 "" 括起来的数字。
【问题讨论】:
-
请edit你的问题标题有意义。在这里发帖的每个人都可以说需要一些帮助,标签上说它是关于 Hive、Apache Spark 和正则表达式的。从你的标题中删除那些无意义的信息使它什么也没说。你的标题应该描述一个特定的问题或提出一个特定的问题,这种方式对浏览搜索结果列表的未来读者有意义。谢谢。
标签:
regex
hive
apache-spark-sql
regex-group
regex-greedy
【解决方案1】:
This RegEx 可能会帮助您获得0.1。它将目标行分为两组,其中第二组 ($2) 返回您想要的浮点数:
("score":")([0-9\.\,]+)
【解决方案2】:
您不需要在 Hive 中使用正则表达式解析 JSON,它具有相同的嵌入式函数:
with your_table as (--use your table instead of this
select '{"reason_desc":
{
"score":"0.1",
"numOfIndicatrix":"0",
"indicatrix":[]},
"success":true,
"id":"1555039965661065S427A2DCF5787920"
}' as json_col
)
select get_json_object(t.json_col,'$.reason_desc.score') as score
from your_table t
结果:
0.1
另请参阅:json_tuple