【问题标题】:Extract a word from the string if the string contains a word from the list如果字符串包含列表中的单词,则从字符串中提取单词
【发布时间】:2021-05-19 14:48:32
【问题描述】:

如果字符串包含列表中的单词,我正在尝试从字符串中提取单词。

比如有字符串“RR_SM_Brand_A_Additive_Clean_jun2020”, 并且列表是 Brand_A、Brand_B、Brand_C 等。

我使用以下正则表达式:

/^([\s\S]*?)(Brand_A|Brand_B|Brand_C)([\s\S]*?)$.*/m

演示:Regex demo

它找到了 Brand_A,但它也有一些其他组。当我在 Google BigQuery 中运行相应的查询时,我收到一个错误:此查询没有返回任何结果。

SELECT distinct utm_campaign,  
REGEXP_EXTRACT(utm_campaign, r'/^([\s\S]*?)(Lysol|Airwick|Finish)([\s\S]*?)$.*/m')
FROM `project.dataset.table`
WHERE utm_campaign = "RR_SM_Brand_A_Additive_Clean_jun2020"

【问题讨论】:

    标签: regex google-bigquery


    【解决方案1】:

    将第一个和最后一个括号替换为(?: ) 集合而不是( )

    /^(?:[\s\S]*?)(Brand_A|Brand_B|Brand_C)(?:[\s\S]*?)$.*/m
    

    这将阻止括号实际返回捕获的组,使它们仅分组,而不是捕获。

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 2023-03-24
      • 1970-01-01
      • 2022-12-01
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多