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.
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 ---.
Found a solution, regex should be:
/---(.|\n)*---/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With