简体   繁体   中英

Extract nested fields in sql using REGEXP_EXTRACT

I am using Google Big Query and querying Sample Records for column in table:

age=18;cntry=us;coid=9911718;csize=c;func=ops;gdr=f;grp=2099628;grp=85824;grp=1548357;grp=88799;grp=2059383;grp=1937629;ind=78;lang=en;mod=0;occ=511;optout=false;reg=21;s=0;seg=9001;seg=761;seg=541;seg=521;seg=1068;seg=557;seg=546;seg=514;seg=504;seg=183;seg=263;sub=0;tile=1;tile_p=1;title=ic;u=nql8uz5qrt8vcqh5tkcqq697

Query:

SELECT REGEXP_EXTRACT(col,r'age=(\d+)') AS age,
  REGEXP_EXTRACT(col,r'cntry=(\d+)') AS country,
  REGEXP_EXTRACT(col,r'gdr=(\d+)') AS gender from table x

Result: 18 null null

I am getting only age value but other values as null. Any help will great on this.

The cntry and gdr values are not numeric, they consist of letters.

You may use \\w+ matching 1 or more "word" chars, ie letters, digits and underscores:

SELECT REGEXP_EXTRACT(col,r'age=(\d+)') AS age,
  REGEXP_EXTRACT(col,r'cntry=(\w+)') AS country,
  REGEXP_EXTRACT(col,r'gdr=(\w+)') AS gender from table x

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