有没有一种方法可以表示 YAML 文档中多行中没有任何空格的长字符串?

发布于 2024-11-14 04:39:01 字数 268 浏览 3 评论 0原文

假设我有以下字符串:

"abcdefghijklmnopqrstuvwxyz"

并且我认为对于 YAML 文件中的一行来说它太长了,是否有某种方法可以将其拆分为几行?

>-
    abcdefghi
    jklmnopqr
    stuvwxyz

会导致 "abcdefghi jklmnopqr stuvwxyz" 很接近,但不应该有任何空格。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2024-11-21 04:39:01

使用双引号,并转义换行符:

"abcdefghi\
jklmnopqr\
stuvwxyz"

Use double-quotes, and escape the newline:

"abcdefghi\
jklmnopqr\
stuvwxyz"
歌枕肩 2024-11-21 04:39:01

杰西的回答可能会忽略一些微妙之处。

YAML(像许多编程语言一样)以不同方式对待单引号和双引号。考虑这个文档:

regexp: "\d{4}"

这将无法解析并出现错误,例如:

在第 1 行第 9 列解析带引号的标量时发现未知转义字符

将其与:

regexp: '\d{4}'

哪个将正确解析。为了在双引号字符串中使用反斜杠字符,您需要对它们进行转义,如下所示:

regexp: "\\d{4}"

我还想强调 Steve 对单引号字符串的评论。考虑这个文档:

s1: "this\
  is\
  a\
  test"

s2: 'this\
  is\
  a\
  test'

解析时,你会发现它相当于:

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

这是 YAML 将单引号字符串视为文字的直接结果,而双引号字符串则受到转义字符扩展的影响。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文