简体   繁体   中英

How can I split a string into two based on a character inbetween using REGEXP_EXTRACT?

I need to split a string into two based on a character and I need to do this without using SPLIT command.

What I have:

string
fruit=orange
fruit=apple
vegetable=onion

What I need:

splitstring1 splitstring2
fruit orange
fruit apple
vegetable onion

How can I solve this with REGEXP_EXTRACT ?

Consider the below query for your requirement:

SELECT
  REGEXP_EXTRACT(string, r'^[a-zA-Z]+') AS splitstring1,
  REGEXP_EXTRACT(string, r'[a-zA-Z]+$') AS splitstring2
FROM
  `project.dataset.table`;

Result:

在此处输入图像描述

EDIT: Based on your new requirement:

  • Sample Input: fruit=apples and oranges

  • Expected Output: "fruit" in one column and "apples and oranges" in another column

Consider the below query:

SELECT
  REGEXP_EXTRACT(string, r'^[a-zA-Z0-9 ]+') AS splitstring1,
  REGEXP_EXTRACT(string, r'[a-zA-Z0-9 ]+$') AS splitstring2
FROM
  `project.dataset.table`;

Result:

在此处输入图像描述

Consider below approach

select 
  arr[offset(0)] as splitstring1,
  arr[safe_offset(1)] as splitstring2
from  your_table,
unnest([struct(regexp_extract_all(string, r'[^=]+') as arr)])          

if applied to sample data in your question - output is

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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