【问题标题】:Biq Query regex_replace error (\? vs \\?)Bigquery regexp_replace 错误(\? vs \\?)
【发布时间】:2018-10-06 08:26:29
【问题描述】:

我无法理解这个正则表达式有什么问题:\?.*

 select REGEXP_REPLACE(longstringcolumn, '\?.*', '') as newstring from tablename

我的示例字符串 aka 'longstring' 有 '?'字符,我正在尝试匹配尾随'? (包括“?”本身)。

我已经在在线工具中检查了我的正则表达式,我的正则表达式似乎可以正常工作。

编辑

感谢大家这么快, 这是一个示例字符串(它是一个 url):

http://example.com/one/two/three?lang=en&region=CN

我试图去掉“?”之后的所有内容。所以这部分:

?lang=en&region=CN

这是我返回的错误:无法解析正则表达式“?”:重复运算符没有参数:?

我真的倾向于这是一个简单的转义字符问题,但我无法以某种方式弄清楚。

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。
  • 另外,我在另一个 regexp_replace() 中使用它。

标签: sql regex google-bigquery


【解决方案1】:
#standardSQL
SELECT REGEXP_REPLACE(longstringcolumn, '\\?.*', '') AS newstring 
FROM tablename  

#standardSQL
SELECT REGEXP_REPLACE(longstringcolumn, r'\?.*', '') AS newstring 
FROM tablename

下面的例子

#standardSQL
WITH tablename AS (
  SELECT 'is this a question?abc ' AS longstringcolumn UNION ALL
  SELECT 'this is not a question' union all
  SELECT'http://example.com/one/two/three?lang=en&region=CN'
)
SELECT REGEXP_REPLACE(longstringcolumn, r'\?.*', '') AS newstring 
FROM tablename  

结果为(在哪里?和所有尾随字符都被删除)

Row newstring    
1   is this a question   
2   this is not a question   

希望这表明您的原始查询出了什么问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 2022-12-02
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 2017-01-12
    • 2019-08-24
    • 1970-01-01
    相关资源
    最近更新 更多