简体   繁体   English

为 Google Big Query 替换正则表达式

[英]Replace a Regex look ahead for Google Big Query

I have some data for which i want to select the LAST VALUE BEFORE THE DELIMITER我有一些数据想要 select 分隔符之前的最后一个值

Example- A -> B -> C -> D示例- A -> B -> C -> D

In this case, i want to select "C" so i used a positive look ahead but BQ doesnt allow look aheads/behinds在这种情况下,我想 select "C" 所以我使用了积极的向前看,但 BQ 不允许向前/向后看

Here is the Regex, (?=[^\n>] -[^\n-] $)[^\n-]*这是正则表达式, (?=[^\n>] -[^\n-] $)[^\n-]*

Can someone help me replace the look ahead?有人可以帮我更换前瞻吗?

You don't need any lookarounds and can use a simple regex like following and extract your needed contents from group1.您不需要任何环顾四周,可以使用如下所示的简单正则表达式并从 group1 中提取您需要的内容。

^.*->(.*)->.*$

Explanation:解释:

  • ^ - Matches start of text ^ - 匹配文本的开头
  • .*-> - Matches any text greedily and second last delimiter .*-> - 贪婪地匹配任何文本和倒数第二个分隔符
  • (.*) -> Captures the text between second last and last delimiter in group1 (.*) -> 捕获 group1 中倒数第二个和最后一个分隔符之间的文本
  • -> -> Matches last delimiter -> -> 匹配最后一个分隔符
  • .*$ -> Matches some text optionally after last delimiter and end of line .*$ -> 在最后一个分隔符和行尾之后可选地匹配一些文本

Check this demo检查这个演示

Consider below options (using regex and split)考虑以下选项(使用正则表达式和拆分)

select col, 
  regexp_extract(col, r'^.*->(.*)->.*$') as extract_with_regex, 
  array_reverse(split(col, ' -> '))[offset(1)] as extract_with_split
from your_table

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM