简体   繁体   English

雪花 SQL 正则表达式

[英]Snowflake SQL Regex

I am trying to identify a value that is nested in a string using Snowflakes regexp_substr()我正在尝试使用 Snowflakes regexp_substr() 识别嵌套在字符串中的值

The value that I want to access is in quotes:我要访问的值用引号引起来:

...
Type:
value: "CategoryA"
...

Edit: This text is nested in a much larger portion of text.编辑:此文本嵌套在更大的文本部分中。

I want to extract CategoryA for all columns using regexp_substr.我想使用 regexp_substr 为所有列提取 CategoryA。 But I am unsure how.但我不确定如何。 I have tried:我试过了:

regexp_substr(col, 'Type\\W+(\\w+)\\W+\\w.+')

and while that gives the portion of the string, I just want what is in quotes and can't figure out how to do so.虽然这给出了字符串的一部分,但我只想要引号中的内容而无法弄清楚如何这样做。

You could use regexp_replace() instead:您可以使用 regexp_replace() 代替:

regexp_replace(col, '(^[^"]*")|("[^"]*$)", '')

The regexp matches on both following conditions, and replaces matching parts with the empty string: regexp 匹配以下两个条件,并用空字符串替换匹配部分:

  • ^[^"]*" : everything from the beginning of the string to the first double quote ^[^"]*" :从字符串开头到第一个双引号的所有内容

  • ("[^"]*$)" : everything from the last double quote to the end of the string ("[^"]*$)" :从最后一个双引号到字符串结尾的所有内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM