[英]Remove YAML header from markdown file
How to remove a YAML header like this one from a text file in Ruby: 如何从Ruby中的文本文件中删除像这样的YAML标头:
---
date: 2013-02-02 11:22:33
title: "Some Title"
Foo: Bar
...
---
(The YAML is surrounded by three dashes (-)) (YAML被三个破折号( - )包围)
I tried 我试过了
text.gsub(/---(.*)---/, '') # text is the variable which contains the full text of the file
but it didn't work. 但它不起作用。
找到解决方案,正则表达式应该是:
/---(.|\n)*---/
The solution mentioned above will match from the first occurrence of ---
to the last occurrence of ---
and everything in between. 上面提到的解决方案将匹配从第一次出现---
到最后一次出现---
以及介于两者之间的一切。 That means if ---
appears later on in your file you'll strip out not only the header, but some of the rest of the content. 这意味着如果---
稍后出现在您的文件中,您不仅会删除标题,还会删除部分其他内容。
This regex will only remove the yaml header: 这个正则表达式只会删除yaml标头:
/\A---(.|\n)*?---/
The \\A
ensures that it starts matching against the very first instance of ---
and the ?
\\A
确保它开始匹配---
第一个实例和?
makes the *
be non-greedy, which makes it stop matching at the second instance of ---
. 使*
非贪婪,这使得它在---
的第二个实例停止匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.