I would like to extract values from a string semicolon separated values that could also contains semicolon but not as separator. The RegEx I found on this website (I lost the post) is almost complete because it separates the key and it's value.
Example:
my.parameter 10; the.foo "Procedural Map"; pve; server.description "This; is \"my\", my description,.\n"
Current result with [^; "]+|"(?:\\"|[^"])*"/g
[
'server.seed',
'10',
'pve',
'server.level',
'"Procedural Map"',
'server.description',
'"This; is \\"my\\", server; description,."'
]
Desired result
[
'my.parameter 10',
'the.foo "Procedural Map"',
'pve',
'server.description "This; is \"my\", server; description,.\n"'
]
Can you help me to improve the RegEx to group the parameter and it's value?
I found a workaround by replacing separator by the ASCII separator (␟) then splitting the result.
const separatorPattern = /; (?=([^"]*"[^"]*")*[^"]*$)/g;
const myRawString = "server.seed 10; server.pve, server.level \"Procedural Map\"; server.description \"This; is \\\"my\\\", server; description,.\"";
const replacedSeparator = myRawString.replace(separatorPattern, "␟");
const parameters = replacedSeparator.split("␟");
console.log(parameters);
/*[
'server.seed 10',
'server.pve, server.level "Procedural Map"',
'server.description "This; is \\"my\\", server; description,."'
]*/
You could use a repeating pattern to first match any char except the ; and then optionally match from an opening till closing double quote and match the escaped double quotes in between.
After that optionally repeat the character class [^";\\]* to also match what comes after the closing double quote.
[^;"\\]+(?:"(?:[^"\\]*(?:\\.[^"\\]*)*)"[^";\\]*)*
[^;"\\]+ Match 1+ times any char except ; " \(?: Non capture group to repeat as a whole
" Match literally(?: Non capture group
[^"\\]* Match 0+ times any char except " \(?:\\.[^"\\]*)* Optionally repeat matching \ and any char followed by 0+ times any char except " and \) Close the non capture group" Match literally[^";\\]* Optionally match any char except " ; \)* Close the outer non capture group and optionally repeat