我想知道我是否可以在BigQuery中使用正则表达式从字符串中提取所有数字。
我认为下面有效但只返回第一击 - 是否有提取所有命中的方法。
我的用例是,我基本上想从网址获取最大数字,因为这更像是我需要加入的post_id。
这是我正在谈论的一个例子:
SELECT
mystr,
REGEXP_EXTRACT(mystr, r'(\d+)') AS nums
FROM
(SELECT 'this is a string with some 666 numbers 999 in it 333' AS mystr),
(SELECT 'just one number 123 in this one ' AS mystr),
(SELECT '99' AS mystr),
(SELECT 'another -2 example 99' AS mystr),
(SELECT 'another-8766 example 99' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/gallery/001' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/print-preview' AS mystr)
我得到的结果是:
[
{
"mystr": "this is a string with some 666 numbers 999 in it 333",
"nums": "666"
},
{
"mystr": "just one number 123 in this one ",
"nums": "123"
},
{
"mystr": "99",
"nums": "99"
},
{
"mystr": "another -2 example 99",
"nums": "2"
},
{
"mystr": "another-8766 example 99",
"nums": "8766"
},
{
"mystr": "http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999",
"nums": "2015"
},
{
"mystr": "http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/gallery/001",
"nums": "2015"
},
{
"mystr": "http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/print-preview",
"nums": "2015"
}
]
答案 0 :(得分:7)
经过一番挖掘,我最终得到了这个解决方案:
SELECT
mystr,
GROUP_CONCAT(SPLIT(REGEXP_REPLACE(mystr, r'[^\d]+', ','))) AS nums
FROM
(SELECT 'this is a string with some 666 numbers 999 in it 333' AS mystr),
(SELECT 'just one number 123 in this one ' AS mystr),
(SELECT '99' AS mystr),
(SELECT 'another -2 example 99' AS mystr),
(SELECT 'another-8766 example 99' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/gallery/001' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/print-preview' AS mystr)
工作原理:
split
获取结果,废弃空结果group_concat
就是在这里展示结果答案 1 :(得分:1)
虽然您将越来越多地在BigQuery中使用Regex,但您会发现它的实现现在非常有限
BigQuery Regular expression functions
re2 Syntax
所以很可能很快你将不得不做类似下面的事情 请注意 - 对于您当前的具体示例 - 以下代码与@Cybril提供的简单解决方案完全没有任何好处 此解决方案更适合您近期的潜在需求 它使用javascript UDF,从而为您提供javascript regexp实现的强大功能 BigQuery User-Defined Functions
SELECT mystr, MAX(number) as max_number FROM JS(
// input table
(SELECT mystr FROM
(SELECT 'this is a string with some 666 numbers 999 in it 333' AS mystr),
(SELECT 'just one number 123 in this one ' AS mystr),
(SELECT '99' AS mystr),
(SELECT 'another -2 example 99' AS mystr),
(SELECT 'another-8766 example 99' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/gallery/001' AS mystr),
(SELECT 'http://somedomain.com/2015/12/this-is-a-post-with-id-in-url-99999/print-preview' AS mystr)
) ,
// input columns
mystr,
// output schema
"[
{name: 'mystr', type: 'string'},
{name: 'number', type: 'string'}
]",
// function
"function(r, emit){
var numbers = r.mystr.match(/(\d+)/g);
for (var i=0; i < numbers.length; i++) {
emit({
mystr: r.mystr,
number: numbers[i]
});
};
}"
)
GROUP BY 1
当然,您也可以在UDF中移动确定最大值的逻辑,以消除额外的分组