I have this regex pattern (written for PHP):
(?:"(?:\\"|[^"])*"|)\K(,)
I need to use this in dotnet, but \K
is not supported. Is there an alternative I can use to produce the same effect?
Edit:
This regex is part of this method:
private static function fix_json(string $json){
if(preg_match('/^\[(.+)\]$/s', $json, $matches) > 0){
$parts = preg_split('/(?:"(?:\\"|[^"])*"|)\K(,)/', $matches[1]); //Splits on commas not inside quotes, ignoring escaped quotes
foreach($parts as $k => $part){
$part = trim($part);
if($part === ""){
$part = "\"\"";
}
$parts[$k] = $part;
}
$fixed = "[" . implode(",", $parts) . "]";
return $fixed;
}
return $json;
}
This method fixes a JSON list with empty items. E.g. from [null,,,]
to [null,"","",""]
. It also should not break with escaped quotation marks inside string values (e.g. [null,"quote \"",]
) and also with commas inside (e.g. [null,,"commas,,",]
)