Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape indicator characters (i.e. : or - ) in YAML

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?

like image 755
danieltahara Avatar asked Jul 02 '12 21:07

danieltahara


People also ask

How do you escape the special characters in YAML?

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.

How do I add special characters to YAML?

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.

How do I get out of backslash in YAML?

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.

How do you escape characters?

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.


1 Answers

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/" 
like image 184
Ry- Avatar answered Nov 01 '22 04:11

Ry-