1

I have to split some string in PostgreSQL on ',' but not on '\,' (backslash is escape character). For example, regexp_split_to_array('123,45,67\,89', ???) must split the string to array {123, 45, "67\,89"}.

What done already: E'(?<!3),' works with '3' as escape character. But how can I use the backslash instead of 3?

Does not work:

E'(?<!\),' does not split the string at all

E'(?<!\\),' throws error "parentheses () not balanced"

E'(?<!\ ),' (with space) splits on all ',' including '\,'

E'(?<!\\ ),' (with space) splits on all ',' too.

Evgeny Nozdrev
  • 1,530
  • 12
  • 15

2 Answers2

4

The letter E in front of the text means C string and then you must escape twice, one for the C string and one for the regexp.

Try with and without E:

regexp_split_to_array('123,45,67\,89', '(?<!\\),')
regexp_split_to_array('123,45,67\,89', E'(?<!\\\\),')

Here http://rextester.com/VEE84838 a running example (unnest() is just for row by row display of results):

select unnest(regexp_split_to_array('123,45,67\,89', '(?<!\\),'));
select unnest(regexp_split_to_array('123,45,67\,89', E'(?<!\\\\),'));
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Emilio Platzer
  • 2,327
  • 21
  • 29
0

You can also split it to groups first:

(\d+),(\d+\,\d+)?

( and later on concatenate them with comma)

tomersss2
  • 135
  • 12