BigQuery中的Regexp

时间:2015-04-30 10:05:03

标签: regex google-bigquery

如何在bigquery中搜索表达式并将它们分组,即使它们被分号搞砸了?

数据库示例:

:Adidas
Adidas
Adidas;
null
adidas
7up
7UP
7UP;
:7UP
null

我想将它们分组并计算。我想得到这个结果:

adidas 4
7up    4
null   2

到目前为止,goup by并没有帮助,我在Excel中完成这项工作,这一切都很快。

1 个答案:

答案 0 :(得分:4)

首先,您需要规范化文本以仅保留有效的单词。下面的正则表达式只是一个简单的表达式,您需要匹配并扩展到您的逻辑。

SELECT normalized,
       count(1) AS c
FROM
  (SELECT label,
          lower(REGEXP_EXTRACT(label,r'[[:punct:]]?([[:^punct:]]*)')) AS normalized
   FROM
     (SELECT string(':Adidas') AS label),
     (SELECT string('Adidas') AS label),
     (SELECT string('Adidas;') AS label),
     (SELECT string(NULL) AS label),
     (SELECT string('adidas') AS label),
     (SELECT string('7up') AS label),
     (SELECT string('7UP') AS label),
     (SELECT string('7UP;') AS label),
     (SELECT string(':7UP') AS label),
     (SELECT string(NULL) AS label),)
GROUP BY normalized
ORDER BY c DESC

此输出:

+-----+------------+---+---+
| Row | normalized | c |   |
+-----+------------+---+---+
|   1 | adidas     | 4 |   |
|   2 | 7up        | 4 |   |
|   3 | null       | 2 |   |
+-----+------------+---+---+