1

I have some data for which i want to select the LAST VALUE BEFORE THE DELIMITER

Example- 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

Here is the Regex, (?=[^\n>]-[^\n-]$)[^\n-]*

Can someone help me replace the look ahead?

2 Answers2

1

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

enter image description here

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
0

You don't need any lookarounds and can use a simple regex like following and extract your needed contents from 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
  • -> -> Matches last delimiter
  • .*$ -> Matches some text optionally after last delimiter and end of line

Check this demo

Update: If there is no delimiter before C then just need to make before part optional using this regex,

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

Check demo where no delimiter before C

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36