在SQL中解析单个属性

时间:2016-04-20 14:16:49

标签: sql string parsing google-bigquery

我有一个描述属性,其中包含以下所有信息。

 Subject:
 Security ID:   SXXX 
 Account Name:  GXXX$
 Account Domain: GGGG
 Security ID:   SXXX0
 Account Name: NETWORK 
 Account Domain: AUTHORITY

工作站名称:

我正在尝试解析此属性并获取其中的不同部分并将其存储在单独的列中。例如,Subject进入一个单独的列,Security ID进入一个单独的列,依此类推。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

请尝试以下作为在BigQuery中使用Regular expression functions的示例

SELECT
  REGEXP_EXTRACT(description, 'Subject:([\\s\\S\\w\\W]*?)Security ID:') AS subject,

  REGEXP_EXTRACT(description, 'Security ID:([\\s\\S\\w\\W]*?)Account Name:') AS securityId_1,
  REGEXP_EXTRACT(description, 'Account Name:([\\s\\S\\w\\W]*?)Account Domain:') AS accountName_1,
  REGEXP_EXTRACT(description, 'Account Domain:([\\s\\S\\w\\W]*?)Security ID:') AS accountDomain_1,

  REGEXP_EXTRACT(description, 'Account Domain:[\\s\\S\\w\\W]*?Security ID:([\\s\\S\\w\\W]*?)Account Name:') AS securityId_2,
  REGEXP_EXTRACT(description, 'Security ID:[\\s\\S\\w\\W]*Account Name:([\\s\\S\\w\\W]*?)Account Domain:') AS accountName_2,
  REGEXP_EXTRACT(description, 'Account Name:[\\s\\S\\w\\W]*Account Domain:([\\s\\S\\w\\W]*)') AS accountDomain_2,

FROM
  (SELECT ' Subject: Test1
   Security ID:   SXXX 
   Account Name:  GXXX$
   Account Domain: GGGG
   Security ID:   SXXX0
   Account Name: NETWORK 
   Account Domain: AUTHORITY' AS description 
  ),
  (SELECT 'Subject: Test2 Security ID: SXXX Account Name: GXXX$ Account Domain: GGGG Security ID: SXXX0 Account Name: NETWORK Account Domain: AUTHORITY ' AS description)