[英]Regex (All after first match (without the first match))
I am struggling with the easy Regex expression.我正在为简单的正则表达式而苦苦挣扎。 Basically I want everything after the first match of "_" without the "_".
基本上我想要没有“_”的“_”的第一场比赛之后的一切。
My current expression is like this: _(.*)
我现在的表达是这样的:
_(.*)
When I give input: AAA_BBB_CCC
当我输入时:
AAA_BBB_CCC
The output is: _BBB_CCC
输出为:
_BBB_CCC
My ideal output would be: BBB_CCC
我的理想输出是:
BBB_CCC
I am using a snowflake database with their build-in regex function.我正在使用带有内置正则表达式函数的雪花数据库。
Unfortunately, I can not use (?<=_).*
as it does not support this format of "?<=".不幸的是,我不能使用
(?<=_).*
因为它不支持这种“?<=”格式。 Is there some other way how can I modify _(.*)
to get the right output?有没有其他方法可以修改
_(.*)
以获得正确的输出?
Thank you.谢谢你。
You can use a regular expression to achieve this, something like this is JavaScript for example will do the job您可以使用正则表达式来实现这一点,例如 JavaScript 之类的东西就可以完成这项工作
"AAA_BBB_CCC".replace(/[^_]+./, '')
Use REGEXP_REPLACE with Snowflake将 REGEXP_REPLACE 与雪花一起使用
regexp_replace('AAA_BBB_CCC','^[^_]+_','')
https://docs.snowflake.net/manuals/sql-reference/functions/regexp_replace.html https://docs.snowflake.net/manuals/sql-reference/functions/regexp_replace.html
But you can also find the first index of _
and use substring, available in all languages但是您也可以找到
_
的第一个索引并使用子字符串,适用于所有语言
let text = "AAA_BBB_CCC"
let index = text.indexOf('_')
if(index !== -1 && index < text.length) {
let result = text.substring(index+1)
}
In Snowflake SQL, you may use REGEXP_SUBSTR
, its syntax is在 Snowflake SQL 中,您可以使用
REGEXP_SUBSTR
,其语法为
REGEXP_SUBSTR( <string> , <pattern> [ , <position> [ , <occurrence> [ , <regex_parameters> [ , <group_num ] ] ] ] )
.REGEXP_SUBSTR( <string> , <pattern> [ , <position> [ , <occurrence> [ , <regex_parameters> [ , <group_num ] ] ] ] )
。
The function allows you to return captured substrings :该函数允许您返回捕获的子字符串:
By default,
REGEXP_SUBSTR
returns the entire matching part of the subject.默认情况下,
REGEXP_SUBSTR
返回主题的整个匹配部分。 However, if thee
(for “extract”) parameter is specified,REGEXP_SUBSTR
returns the the part of the subject that matches the first group in the pattern.但是,如果指定了
e
(用于“提取”)参数,则REGEXP_SUBSTR
返回与模式中的第一组匹配的主题部分。 Ife
is specified but agroup_num
is not also specified, then thegroup_num
defaults to 1 (the first group).如果指定了
e
但未指定group_num
,则group_num
默认为 1(第一组)。 If there is no sub-expression in the pattern, REGEXP_SUBSTR behaves as if e was not set.如果模式中没有子表达式,则 REGEXP_SUBSTR 的行为就像没有设置 e 一样。
So, you need to set the regex_parameters to e
and - optionally - group_num
argument to 1
:因此,您需要将regex_parameters设置为
e
并将 - 可选 - group_num
参数设置为1
:
Select REGEXP_SUBSTR('AAA_BBB_CCC', '_(.*)', 1, 1, 'e', 1)
Select REGEXP_SUBSTR('AAA_BBB_CCC', '_(.*)', 1, 1, 'e')
Use a capture group:使用捕获组:
\_(?<data>.*)
Which returns the capture group data
containing BBB_CCC
它返回包含
BBB_CCC
的捕获组data
Example: https://regex101.com/r/xZaXKR/1示例: https : //regex101.com/r/xZaXKR/1
To get this actually working you need to use:要使其实际工作,您需要使用:
SELECT REGEXP_SUBSTR('AAA_BBB_CCC', '_(.*)', 1, 1, 'e', 1);
which gives:这使:
REGEXP_SUBSTR('AAA_BBB_CCC', '_(.*)', 1, 1, 'E', 1)
BBB_CCC
you need to pass the REGEXP_SUBSTR parameter <regex_parameters>
clause of e
as that is extract sub-matches.
您需要传递
e
的REGEXP_SUBSTR参数<regex_parameters>
子句,因为这是extract sub-matches.
thus Wiktor's answer is 95% correct.因此,Wiktor 的答案是 95% 正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.