Darken Aki
Darken Aki

Reputation: 3

Separate json key : value by comma, but with not closed quotes inside value quotes

For example I have this json:

{"a":"some value", "b":"some value", "c": "some ,\" value"}

I need to get:

"a":"some value" and "b":"some value" and "c": "some ," value"

I have ended with this regex (,)(?=(?:[^\"]|\"[^\"]*\")*$), but this doesn't work with third key value pair.

Upvotes: 0

Views: 480

Answers (1)

MonkeyZeus
MonkeyZeus

Reputation: 20737

If you really must do this with regex then you can try:

(".*?(?<!\\)")\s*:\s*(".*?(?<!\\)")
  • (".*? - start a capture group and match an opening double-quote and lazily match zero or more of any char
  • (?<!\\) - make sure that a \ does not precede the closing double-quote
  • ") - find a closing double-quote and close the capture group
  • \s*:\s* - match a colon : surrounded by optional whitespace
  • (".*?(?<!\\)") - see bullet points 1 through 3

https://regex101.com/r/25qa84/1

Upvotes: 1

Related Questions