使用Big Query / Google Analytics的Regexp_extract将字符A中的子字符串提取到字符B或EOL

时间:2016-02-11 16:00:21

标签: sql regex perl google-analytics

我正在使用Google Big Query并尝试使用Regexp_extract从字符串列中将一些信息提取到另一列中。简而言之:

myVariable 中的数据:

yippie/eggs-spam/?portlet:hungry=1234
yippie/eggs-spam/?portlet:hungry=456&portlet:hungrier=7890

我想要一个列:

1234
456

我的命令:

SELECT Regexp_extract(myVariable, r'SOME_MAGIC') as result
FROM table

我尝试过SOME_MAGIC:

hungry=(.*)[&$] - null, 456 (I learned that $ is interpreted as is)
hungry=(.*)(&|$) - Error: Exactly one capturing group must be specified
hungry=(.*)^& - null, null
hungry=(&.*)?$ - null, null

我看了this,但那里的数字有固定的长度。另外看了this,但是“?=”并不是perl的已知命令。

有人有想法吗?提前谢谢!

2 个答案:

答案 0 :(得分:1)

我刚刚找到了一个如何以不同方式解决问题的答案:

hungry=([0-9]+) - 1234, 456

这不是我抽象问题的答案(选择Charater A到[字符B或EOL]的正则表达式),所以它并不令人满意。例如。它不适用于

yippie/eggs-spam/?portlet:hungry=12AB34

然而我原来的问题已经解决了。如果有人有更好的答案,我会暂时搁置这个问题。

答案 1 :(得分:1)

我想我遇到了类似的问题,我试图将字符串中的最后6个字符(link_id)选为新列。

我一直收到这个错误:

  

必须指定一个捕获组

我的代码最初是:

SELECT
...
REGEXP_EXTRACT(link_id, r'......$') AS updated_link_id
FROM sometable;

为了摆脱错误并将正确的子字符串作为列检索,我必须在我的正则表达式字符串周围添加括号。

SELECT
...
REGEXP_EXTRACT(link_id, r'(......$)') AS updated_link_id
FROM sometable;