2

I have a string filled with data like these that I want to split based on comma :

16588, 16503, 1, 1, N'1, 2, 3 RUE MENESTRELS', 12, 153

In this exemple I want the output to be like these :

{16588|16503|1|1|N'1, 2, 3 RUE MENESTRELS'|12|153}

The fact is that if I do a split based upon a comma, I'll get this result :

{16588|16503|1|1|N'1|2|3 RUE MENESTRELS'|12|153}

I think a regex can solve this, but after searching for a while I haven't found a solution yet... (And I must confess that I'm not very good with regex too)

To summarize, I'd like to split a string based on all comma, except comma between the characters N' and the characters '

I tried to use this sample but it doesn't work

\,(?!N[^']*')

Sorry for mistakes, English is not my first language and thank you in advance !

Egan Wolf
  • 3,533
  • 1
  • 14
  • 29
  • 1
    Possible duplicate of [Regex to pick commas outside of quotes](https://stackoverflow.com/questions/632475/regex-to-pick-commas-outside-of-quotes) – Steve Jul 18 '17 at 10:33
  • What language or tool are you using this regex pattern in? You are saying `split` but you are displaying with pipes and wrapping in curly brackets. Can you better explain what you are doing? – mickmackusa Jul 18 '17 at 11:45

3 Answers3

1

This should help you:

(,)(?=(?:[^']|'[^']*')*$)

Demo

Egan Wolf
  • 3,533
  • 1
  • 14
  • 29
0

I think you're after this:

(N'.*?')

See it here Regex101

Mr Mystery Guest
  • 1,464
  • 1
  • 18
  • 47
0

If your tool/language permits (*SKIP) AND (*FAIL), my pattern will provide the same result with a quarter of the effort of Egan's.

/('[^']*'(*SKIP)(*FAIL))|,/    #just 105 steps

Demo Link

The first alternative matches substrings between single quotes then discards them. This leaves the second alternative to correctly match the remaining/qualifying commas.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136