[英]how to pass in a column value in bigquery regexp_contains
以下查询不适用于 containsy 但适用于 containsx 和 containsz 我希望能够使用单词边界使用通配符更灵活地进行搜索和替换。 我知道我能做到' '||我们||' ' 如REGEXP_CONTAINS((b.old_string),(' '||a.search||' ')) containsx.
我不确定这是否是正确的语法r'(?i)(\b.'||a.search||'.\b)')
我希望能够替换我们但不是当它是一部分时另一个词。
WITH t1 AS (
select 'us' search, 'united states' replacex
) ,
t2 as (
select 'usa us bus' old_string
)
select REGEXP_CONTAINS((b.old_string),(a.search)) containsx, --working
REGEXP_CONTAINS((b.old_string),r'(?i)(\b.'||a.search||'.\b)') containsy, --not working
REGEXP_CONTAINS((b.old_string),r'(?i)(\b.us.\b)') containsz, --working
old_string,
replace (b.old_string,a.search,a.replacex) new_string,
search,
replacex
from t1 a
cross join t2 b
where REGEXP_CONTAINS((b.old_string),(a.search));
包含x | 包容的 | 包含z | 旧字符串 | 新字符串 | 搜索 | 替换x |
---|---|---|---|---|---|---|
真的 | 错误的 | 真的 | 美国巴士 | 美国a美国bunit states | 我们 | 美国 |
预计 OUTPUT
包含x | 包容的 | 包含z | 旧字符串 | 新字符串 | 搜索 | 替换x |
---|---|---|---|---|---|---|
真的 | 错误的 | 真的 | 美国巴士 | 美国 美国巴士 | 我们 | 美国 |
WITH t1 AS (
select 'us' search, 'united states' replacex
) ,
t2 as (
select 'usa us bus' old_string union all
select 'I must go to the us' union all
select 'there is no country here' union all
select 'I blew a fuse in the us'
)
select
REGEXP_CONTAINS((b.old_string),CONCAT(r'(?i)(\b', a.search, r'\b)')) is_contains,
old_string,
regexp_replace (b.old_string, CONCAT(r'(?i)(\b', a.search, r'\b)'),a.replacex) new_string,
replacex
from t1 a
cross join t2 b
where REGEXP_CONTAINS((b.old_string),CONCAT(r'(?i)(\b', a.search, r'\b)')) ;
is_contains | 旧字符串 | 新字符串 | 替换x |
---|---|---|---|
真的 | 美国巴士 | 美国 美国巴士 | 美国 |
真的 | 我必须go给我们 | 我必须go到美国 | 美国 |
真的 | 我在美国烧断了保险丝 | 我在美国烧了一根保险丝 | 美国 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.