0 votes

Hi, I'm trying to make a script to make the output of the Dictionary.to_json in a human readable JSON format, instead of the current single line output.
I'm currently stuck on finding only the commas that are not inside quotes. I found this RegEx that should be able to do this:
Regex101 test
The example text is exactly my current test json in use.
Since Godot don't work with regex literals, we have to escape some things and put inside quotes, so I believe I need this:

RegEx.compile("(?!\\B\"[^\"]*),(?![^\"]*\"\\B)")

However, this is currently capturing all commas regardless of quotes, resulting on this newline on the wrong place: Results
Am I missing something or is there something broken with the RegEx? I tried searching issues on github and the Godot Q&A section and found nothing so far.
Thanks in advance, if I manage to find a solution I'll post here.

in Engine by (15 points)
edited by

1 Answer

+1 vote
Best answer

This regex splits on commas but maintains integrity of quotes for me in python, it might be of help?

re.split(r', (?=(?:"[^"]*?(?: [^"]*)*))|, (?=[^",]+(?:,|$))', myStr)

by (204 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.