Nei*_*l P 3 snowflake-cloud-data-platform
我有一个具有多个匹配组的正则表达式。
在雪花中如何指定返回哪个匹配组?
我正在使用REGEXP_SUBSTR,但很乐意使用替代品,如果它们效果更好的话。
TL;DR:不能完全做到这一点,但您可以选择'e'并使用带有 的非捕获组(?:re)。
所以澄清一下,尼尔似乎在要求一些可以回报的word东西
select regexp_substr('bird is the word','(bird) (is) (the) (word)',1,4)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我认为 Snowflake 目前并不完全支持此功能。REGEXP_SUBSTR有一个'e'(提取)参数,它允许您仅提取一个组,但它始终提取第一个组。原因是occurrence今天的参数意味着整个正则表达式在字符串中的出现。例子
select regexp_substr('bird is cows are','([a-z]*) (is|are)',1,2,'e');
=> cows
Run Code Online (Sandbox Code Playgroud)
您可以通过在您想要的结果之前不使用分组来实现您想要的结果,例如
select regexp_substr('bird is the word','bird (is) (the) (word)',1,1,'e');
-> is
select regexp_substr('bird is the word','bird is the (word)',1,1,'e');
-> word
Run Code Online (Sandbox Code Playgroud)
但是,如果您想使用分组来表达替代方案,则这不起作用,例如
select regexp_substr('cow is the word','(bird|cow) is the (word)',1,1,'e');
-> cow
Run Code Online (Sandbox Code Playgroud)
不过,我认为提供提取特定组编号的选项是有价值的,将通过 Snowflake 开发来提高它:)