Postgresql - Regex split csv line with potentials quotes

我与影子孤独终老i 提交于 2019-12-02 03:06:49

问题


I would like to split a column that represent a csv line in postgres. Fields in this text line are delimited by pipe, sometime they are enclosed by quote and sometime not. In addition we can have escaped chars.

field1|"field2"|field3|"22 \" lcd \| screen "

Is there a regex to split this column (i.e. using regexp_split_to_array(....)? )


回答1:


Not about regexp but it works

create or replace function split_csv(
  line text,
  delim_char char(1) = ',',
  quote_char char(1) = '"')
returns setof text[] immutable language plpythonu as $$
  import csv
  return csv.reader(line.splitlines(), quotechar=quote_char, delimiter=delim_char, skipinitialspace=True, escapechar='\\')
$$;

select *, x[4] from split_csv('field1|"field2"|field3|"22 \" lcd \| screen "'||E'\n'||'a|b', delim_char := '|') as x;
╔══════════════════════════════════════════════╤════════════════════╗
║                      x                       │         x          ║
╠══════════════════════════════════════════════╪════════════════════╣
║ {field1,field2,field3,"22 \" lcd | screen "} │ 22 " lcd | screen  ║
║ {a,b}                                        │ ░░░░               ║
╚══════════════════════════════════════════════╧════════════════════╝


来源:https://stackoverflow.com/questions/42009468/postgresql-regex-split-csv-line-with-potentials-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!