In a config file, I have a key to which I wish to assign a URL. The problem is that YAML interprets : and - characters as either creating mappings or lists, so it has a problem with the line
url: http://www.example-site.com/
(both because of the colon following http and the hyphen in the middle)
Is there an explicit way to escape ':' and '-' ? Or would it work to just put the whole thing in single quotes and call it a day?
When double quotes, "...." , go around a scalar string you use backslash ( \ ) for escaping, and you have to escape at least the backslash and the double quotes. In addition you can escape other special characters like linefeed ( \n ) and escape an end-of-line by preceding it by a backslash.
YAML doesn't require quoting most strings but you'll want to quote special characters if they fall within a certain list of characters. Use quotes if your value includes any of the following special characters: { , } , [ , ] , & , * , # , ? , | , - , < , > , = , ! , % , @ , : also ` and , YAML Spec.
In YAML, text scalars can be surrounded by quotes enabling escape sequences such as \n to represent a new line, \t to represent a tab, and \\ to represent the backslash.
Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped. Note: If you use braces to escape an individual character within a word, the character is escaped, but the word is broken into three tokens.
Quotes:
"url: http://www.example-site.com/"
To clarify, I meant “quote the value” and originally thought the entire thing was the value. If http://www.example-site.com/
is the value, just quote it like so:
url: "http://www.example-site.com/"
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