简体   繁体   English

我想了解如何在 BigQuery 中使用 REGEXP_EXTRACT 函数

[英]I am trying to understand how to use the REGEXP_EXTRACT function in BigQuery

I have a column string value:我有一个列字符串值:

SELFSERVE TICKET\STATION\TRAINCOMPANY\GBR

I would like to extract the part of the string before the first "\\" ie SELFSERVE TICKET我想提取第一个“\\”之前的字符串部分,即SELFSERVE TICKET

What would be the right syntax to achieve this?实现这一目标的正确语法是什么? If there are better functions to use I am ears.如果有更好的功能可以使用我是耳朵。

I don't think that a regex is necessary here.我认为这里不需要正则表达式。 You can just use regular string functions:您可以只使用常规字符串函数:

substr(mycol, 1, strpos(mycol, '\') - 1)

This gives you everything before the first \\ .这为您提供了第一个\\之前的所有内容。

If you have strings that do not contain \\ at all, and you want to return the entire string in that case:如果您的字符串根本不包含\\ ,并且您想在这种情况下返回整个字符串:

substr(concat(mycol, '\'), 1, strpos(mycol, '\') - 1)

Finally, if you want to get all parts at once, you can use split() , which returns an array of strings:最后,如果你想一次获取所有部分,你可以使用split() ,它返回一个字符串数组:

split(mycol, '\')

在 BigQuery 中,我会使用:

select split(col, '\')[ordinal(1)] as first_value

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

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