jeudi 9 juin 2016

Ruby get blocks via regex

When using scan in ruby, we are searching for a block within a textfile.

ex file:

sometextbefore
begin
  sometext
end
sometextafter
begin
  sometext2
end
sometextafter2

we want the following result in array:

["begin\nsometext\nend","begin\nsometext2\nend"]

with this scan method:

textfile.scan(/begin\s.(.*?)end/m)

we get:

["sometext","sometext2"] 

We want the begin and end still in the output, not cutted off. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire