0

I am using ORM which doesn't support postgres array, so I am trying to do some hacks to add "support".

Currently I have to convert postgres array string into array of programming language.

Example of postgres array string representation:

{"bla, bla",bla,"bu bu",bu}

So if there are spaces, postgres is automatically adding quotes, if not, then element is without quotes.

What regex you would use to get array out of this? So result should be:

array := []{"bla, bla", "bla", "bu bu", "bu"}

I am using Go.

Arrays are one dimensional, so something like: CREATE TABLE test ( something text[] );

5
  • 1
    What language do you use? How are quotes escaped? How would these values look with and without quotes: a,b, a"b? Can arrays contain objects or nested arrays? Looks like has multidimensional arrays. Commented Jun 3, 2015 at 8:19
  • With what language are you running the regex replace? Can values have commas within them? Commented Jun 3, 2015 at 8:25
  • Updated question. Language is golang, strings can contain commas. In the first version we just split by comma, but now we can have also commas inside elements, so our old method doesn't work anymore. Commented Jun 3, 2015 at 8:28
  • Which ORM are you using? Commented Jun 3, 2015 at 15:49
  • github.com/jinzhu/gorm Commented Jun 3, 2015 at 20:55

1 Answer 1

1

I'm not au fait with golang, but use this search regex

(?<=,)(?=[^"](([^"]*"){2})*[^"]*$)|(?<=[^"])(?=,(([^"]*"){2})*[^"]*$)

and replace matches with a quote ".

See regex live demo.

Then add "[]" at the front.

Assumes (escaped) quotes never appear within values.

Sign up to request clarification or add additional context in comments.

2 Comments

Question updated. There are commas inside values, and we are using Go.
@user232343 thx. I too have updated to cater for embedded commas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.