sql - Bigquery regexp_replace 错误 (\? vs\\?)

标签 sql regex google-bigquery

我无法理解此正则表达式的问题:\?.*

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

我的示例字符串又名“长字符串”有“?”字符,并且我试图匹配所有尾随“?”的内容(包括“?”本身)。

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

编辑

谢谢你们这么快, 这是一个示例字符串(它是一个 url):

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

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

?lang=en®ion=CN

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

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

最佳答案

#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   

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

关于sql - Bigquery regexp_replace 错误 (\? vs\\?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50029408/

相关文章:

javascript - 正则表达式 - 选择第一个空白字符之前的所有字符,假设它同时包含字符和数字

csv - 将 csv 文件加载到 BQ - 位置参数太多

sql - 在 Power BI 中映射数据库架构

mysql - 确保表构建正确

sql - PG 功能故障排除

google-bigquery - BigQuery 不处理分区列的毫秒时间戳

date - BigQuery - DATE_TRUNC 错误

sql - PostgreSQL 使用 RETURNING 缓慢插入单行(在生产中花费 500 毫秒)

regex - HTML5 输入模式允许以逗号分隔千位

regex - CSV(在字段值中有额外的引号)到 ColdFusion 中的数组