开发者

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

开发者 https://www.devze.com 2023-03-11 20:15 出处:网络
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?

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, bu开发者_高级运维t it shouldn't have any spaces.


Use double-quotes, and escape the newline:

"abcdefghi\
jklmnopqr\
stuvwxyz"


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消