用于获取文件路径直到给定斜杠数字的正则表达式
Regexp expression for getting the file path till a given slash number
我有一个日志 table,其中一行 'path' 的值类似于 root/home/desktop/parent/child/grandchild
我想根据一些整数输入 'n' 对这一行进行分组,其中n 是我们要提取子字符串的斜杠数,然后对其进行分组。例如:在这种情况下,如果 n = 1,如果 n 为 3,我想按 'root/' 分组,如果想按 'root/home/desktop/' 分组。我怎样才能在 BigQuery 中实现这一点?我可以使用相同的正则表达式还是有更好的方法来实现这一点?无论采用何种方法,都希望能给出一些解释。谢谢!!
不确定下面的例子是否真的需要任何额外的解释
select *,
split(path, '/')[safe_offset(0)],
split(path, '/')[safe_offset(1)],
split(path, '/')[safe_offset(2)],
split(path, '/')[safe_offset(3)],
split(path, '/')[safe_offset(4)],
split(path, '/')[safe_offset(5)]
from your_table
有输出
I would like to have the splits combined in the form of a string until the last slash ...
从头开始获取部分路径 - 使用下面的示例
create temp function get_path(path string, n int64) as ((
select string_agg(part, '/' order by offset)
from unnest(split(path, '/')) part with offset
where offset < n
));
select
get_path(path, 1) n1,
get_path(path, 2) n2,
get_path(path, 3) n3,
get_path(path, 4) n4,
get_path(path, 5) n5,
get_path(path, 6) n6
from your_table
输出如下
如果您想使用正则表达式 - 请考虑以下内容
create temp function get_path(path string, n int64) as ((
regexp_extract(path, r'(^(?:[^/]+/?){' || n || '})')
));
with your_table as (
select 'root/home/desktop/parent/child/grandchild' path
)
select
get_path(path, 1) n1,
get_path(path, 2) n2,
get_path(path, 3) n3,
get_path(path, 4) n4,
get_path(path, 5) n5,
get_path(path, 6) n6,
from your_table
有输出
我有一个日志 table,其中一行 'path' 的值类似于 root/home/desktop/parent/child/grandchild
我想根据一些整数输入 'n' 对这一行进行分组,其中n 是我们要提取子字符串的斜杠数,然后对其进行分组。例如:在这种情况下,如果 n = 1,如果 n 为 3,我想按 'root/' 分组,如果想按 'root/home/desktop/' 分组。我怎样才能在 BigQuery 中实现这一点?我可以使用相同的正则表达式还是有更好的方法来实现这一点?无论采用何种方法,都希望能给出一些解释。谢谢!!
不确定下面的例子是否真的需要任何额外的解释
select *,
split(path, '/')[safe_offset(0)],
split(path, '/')[safe_offset(1)],
split(path, '/')[safe_offset(2)],
split(path, '/')[safe_offset(3)],
split(path, '/')[safe_offset(4)],
split(path, '/')[safe_offset(5)]
from your_table
有输出
I would like to have the splits combined in the form of a string until the last slash ...
从头开始获取部分路径 - 使用下面的示例
create temp function get_path(path string, n int64) as ((
select string_agg(part, '/' order by offset)
from unnest(split(path, '/')) part with offset
where offset < n
));
select
get_path(path, 1) n1,
get_path(path, 2) n2,
get_path(path, 3) n3,
get_path(path, 4) n4,
get_path(path, 5) n5,
get_path(path, 6) n6
from your_table
输出如下
如果您想使用正则表达式 - 请考虑以下内容
create temp function get_path(path string, n int64) as ((
regexp_extract(path, r'(^(?:[^/]+/?){' || n || '})')
));
with your_table as (
select 'root/home/desktop/parent/child/grandchild' path
)
select
get_path(path, 1) n1,
get_path(path, 2) n2,
get_path(path, 3) n3,
get_path(path, 4) n4,
get_path(path, 5) n5,
get_path(path, 6) n6,
from your_table
有输出