Re: Regular expression that splits CSV string into table

From: bricklen <bricklen(at)gmail(dot)com>
To: Nick <nboutelier(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regular expression that splits CSV string into table
Date: 2010-09-10 23:43:58
Message-ID: AANLkTik0HfFk7X_SeyNkzj9E502X_bDKzpRGNTbhheFc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Sep 10, 2010 at 3:43 PM, Nick <nboutelier(at)gmail(dot)com> wrote:
> What would be the regexp_split_to_table pattern that splits a comma
> separated string into a table? Im having trouble when a string
> contains commas or there are commas at the beginning or end
>
> String
> ',one,two,''three,four'',five,six,'
>
> Should return
> ,one
> two
> three,four
> five
> six,

You can roll your own function, or try regexp_split_to_table, though
you will probably have to use a different delimiter if you don't want
it to break on "three,four".
Eg.

select regexp_split_to_table('"one","two","three,four","five"',',');
regexp_split_to_table
-----------------------
"one"
"two"
"three
four"
"five"

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2010-09-10 23:53:38 Re: Post Install / Secure PostgreSQL
Previous Message Nick 2010-09-10 22:43:20 Regular expression that splits CSV string into table