简体   繁体   English

如何获取bigquery中字符串中正则表达式的匹配计数?

[英]How to get count of matches of a regexp in a string in bigquery?

How would one go about getting the count of matches of a reqular expression in a string in bigquery? 如何在bigquery中获取字符串中的reqular表达式的匹配计数? I don't think bigquery sql supports regexp_count. 我不认为bigquery sql支持regexp_count。

So, for example, you might want to count the matches of "foo* " in a string like "foo1 foo foo40" and it would return a count of 2 (in this case, I am meaning that the "*" must be a non-empty string). 因此,例如,您可能想在类似“ foo1 foo foo40”的字符串中计算“ foo *”的匹配项,并且它返回的计数为2(在这种情况下,我的意思是“ *”必须为a非空字符串)。

Thanks! 谢谢!

Below is for BigQuery Standard SQL 以下是BigQuery标准SQL

You can use combination of array_length and regexp_extract_all as in example below 您可以使用array_length和regexp_extract_all的组合,如下例所示

#standardSQL
WITH `project.dataset.table` AS (
SELECT "foo1 foo foo40" str
)
SELECT array_length(regexp_extract_all(str, r'foo[^\s]')) matches
FROM `project.dataset.table`   

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

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