Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to represent a long string that doesnt have any whitespace on multiple lines in a YAML document?

Tags:

string

yaml

Say I have the following string:

"abcdefghijklmnopqrstuvwxyz" 

And I think its too long for one line in my YAML file, is there some way to split that over several lines?

>-     abcdefghi     jklmnopqr     stuvwxyz 

Would result in "abcdefghi jklmnopqr stuvwxyz" which is close, but it shouldn't have any spaces.

like image 380
Jake Avatar asked Jun 07 '11 16:06

Jake


People also ask

Does YAML support multiline string?

Find the right syntax for your YAML multiline strings. There are two types of formats that YAML supports for strings: block scalar and flow scalar formats. (Scalars are what YAML calls basic values like numbers or strings, as opposed to complex types like arrays or objects.)

What does a pipe mean in YAML?

The pipe symbol at the end of a line in YAML signifies that any indented text that follows should be interpreted as a multi-line scalar value. See the YAML spec. Specifically, the pipe indicates that (except for the indentation) the scalar value should be interpreted literally in such a way that preserves newlines.

How do you escape quotes in YAML?

In single quoted strings the single quote character can be escaped by prefixing it with another single quote, basically doubling it. Backslashes in single quoted strings do not need to be escaped. Quoted strings can also span across multiple lines, just indent the following lines.

How do I use special characters in YAML?

Use quotes in YAML if your value includes special characters. For example, these special characters may require quotes: {, }, [, ], ,, &, :, *, #, ?, |. -, <. >, =, !, %, @, \. It is not necessary to use quotes when a single special character is surrounded by spaces, for example, * with spaces on both sides.


2 Answers

Use double-quotes, and escape the newline:

"abcdefghi\ jklmnopqr\ stuvwxyz" 
like image 178
Jesse Beder Avatar answered Sep 24 '22 23:09

Jesse Beder


There are some subtleties that Jesse's answer will miss.

YAML (like many programming languages) treats single and double quotes differently. Consider this document:

regexp: "\d{4}" 

This will fail to parse with an error such as:

found unknown escape character while parsing a quoted scalar at line 1 column 9

Compare that to:

regexp: '\d{4}' 

Which will parse correctly. In order to use backslash character inside double-quoted strings you would need to escape them, as in:

regexp: "\\d{4}" 

I'd also like to highlight Steve's comment about single-quoted strings. Consider this document:

s1: "this\   is\   a\   test"  s2: 'this\   is\   a\   test' 

When parsed, you will find that it is equivalent to:

s1: thisisatest s2: "this\\ is\\ a\\ test" 

This is a direct result of the fact that YAML treats single-quoted strings as literals, while double-quoted strings are subject to escape character expansion.

like image 36
larsks Avatar answered Sep 24 '22 23:09

larsks