I want to escape comma, space & double quotes from a YAML file.
How can I do that?
There are multiple ways to include those three characters in a YAML scalar, but if you want to do so using escaping, you have to realise you only can escape the double quote and not the comma or space.
To do so you need to use a double quoted scalar:
"this is a YAML document, with embedded space, comma and escaped double quote (\")"
If however your spaces are not at the beginning or the end, and your double quote not at the beginning of the scalar, you don't need quotes at all. The above is equivalent to the plain scalar:
this is a YAML document, with embedded space, comma and escaped double quote (")
Plain scalars have some restrictions, e.g. you cannot have any of the special characters that start special constructs (among others {, [, & and *) at the beginning, something that looks like the beginning of a comment ( space + # ) or the value indicator (: followed by space) in the middle of plain scalar.
There are also single quoted and block style literal and folded scalars with their own rules.
If unsure I recommend using double quotes. If you load-then-dump that YAML, your library is likely to remove any superfluous quoting the first time you do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With