How to remove a YAML header like this one from a text file in Ruby:
---
date: 2013-02-02 11:22:33
title: "Some Title"
Foo: Bar
...
---
(The YAML is surrounded by three dashes (-))
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:
/\A---(.|\n)*?---/
The \\A
ensures that it starts matching against the very first instance of ---
and the ?
makes the *
be non-greedy, which makes it stop matching at the second instance of ---
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.