以下是 BigQuery 标准 SQL
假设您的行中字段的顺序设置为您的示例中的设置
#standardSQL
WITH raw_messages AS (
SELECT lines
FROM `my_table`
WHERE REGEXP_CONTAINS(lines, '^Date of application: ')
)
SELECT
REGEXP_EXTRACT(lines, r'(?i)^Date of application: ([0-9]{2}/[0-9]{2}/[0-9]{4})') AS DATE,
REGEXP_EXTRACT(lines, r'(?i) Request: (.*?) Contact: ') AS request,
REGEXP_EXTRACT(lines, r'(?i) Contact: (.*?) email: ') AS contact,
REGEXP_EXTRACT(lines, r'(?i) email: (.*?) Tel: ') AS email,
REGEXP_EXTRACT(lines, r'(?i) Tel: (.*?) Ordered inquiry: ') AS phone,
REGEXP_EXTRACT(lines, r'(?i) Order ID: (.*?) BL: ') AS id,
REGEXP_EXTRACT(lines, r'(?i) BL: (.*?) Product: ') AS bl,
REGEXP_EXTRACT(lines, r'(?i) Product: (.*?)$') AS product
FROM raw_messages
您可以使用您问题中的虚拟数据进行测试,如下所示
#standardSQL
WITH `project.dataset.my_table` AS (
SELECT 'Date of application: 01/02/2018 Request: Buy books Contact: email: hi@gmail.com Tel: 0123456789 Ordered inquiry: Order ID: 12345678 BL: 87654321 Product: 123456 Books' lines UNION ALL
SELECT 'Date of application: 01/04/2018 Request: Retour table Contact: Rodion Raskólnikov email: hello@outlook.com Tel: 9876543210 Ordered inquiry: Order Id: 87654321 BL: 12345678 Product: 654321 Tables'
), raw_messages AS (
SELECT lines
FROM `project.dataset.my_table`
WHERE REGEXP_CONTAINS(lines, '^Date of application: ')
)
SELECT
REGEXP_EXTRACT(lines, r'(?i)^Date of application: ([0-9]{2}/[0-9]{2}/[0-9]{4})') AS DATE,
REGEXP_EXTRACT(lines, r'(?i) Request: (.*?) Contact: ') AS request,
REGEXP_EXTRACT(lines, r'(?i) Contact: (.*?) email: ') AS contact,
REGEXP_EXTRACT(lines, r'(?i) email: (.*?) Tel: ') AS email,
REGEXP_EXTRACT(lines, r'(?i) Tel: (.*?) Ordered inquiry: ') AS phone,
REGEXP_EXTRACT(lines, r'(?i) Order ID: (.*?) BL: ') AS id,
REGEXP_EXTRACT(lines, r'(?i) BL: (.*?) Product: ') AS bl,
REGEXP_EXTRACT(lines, r'(?i) Product: (.*?)$') AS product
FROM raw_messages
结果
Row DATE request contact email phone id bl product
1 01/02/2018 Buy books null hi@gmail.com 0123456789 12345678 87654321 123456 Books
2 01/04/2018 Retour table Rodion Raskólnikov hello@outlook.com 9876543210 87654321 12345678 654321 Tables