首页 > 解决方案 > 如何为共享相同根的项目列表编写正则表达式

问题描述

所以我有一个关键字列表: ['xxxxl','xxxl','xxl','xl','xxxxt','xxxt','xxt','xt'] 在 bigquery 中,我想在下面的 sql 代码中编写一个正则表达式

SELECT my_column
FROM table
REGEXP_CONTAINS(lower(my_column),regex)

这样我的输出表只包含与关键字列表中的任何项目都不匹配的值。

谢谢

标签: regexgoogle-bigquery

解决方案


以下是 BigQuery 标准 SQL

#standardSQL
WITH `project.dataset.lookup_table` AS (
  SELECT ['xxxxl','xxxl','xxl','xl','xxxxt','xxxt','xxt','xt'] keywords
)
SELECT my_column
FROM `project.dataset.table`, 
  (SELECT STRING_AGG(LOWER(keyword), '|') exclude_pattern 
  FROM `project.dataset.lookup_table`, 
  UNNEST(keywords) keyword)
WHERE NOT REGEXP_CONTAINS(LOWER(my_column), exclude_pattern)  

您可以使用下面的简化示例测试/玩上面

#standardSQL
WITH `project.dataset.lookup_table` AS (
  SELECT ['xxxxl','xxxl','xxl','xl','xxxxt','xxxt','xxt','xt'] keywords
), `project.dataset.table` AS (
  SELECT 'xxxxl' my_column UNION ALL
  SELECT 'abc'
)
SELECT my_column
FROM `project.dataset.table`, 
  (SELECT STRING_AGG(LOWER(keyword), '|') exclude_pattern 
  FROM `project.dataset.lookup_table`, 
  UNNEST(keywords) keyword)
WHERE NOT REGEXP_CONTAINS(LOWER(my_column), exclude_pattern)   

带输出

Row my_column    
1   abc  

推荐阅读