On 11/15/2011 05:23 PM, Intransition wrote:
> Any expert regexp wrangler around here. I can't quite find a way to
> split a document up in sections:
>
> This is an example.
> a = 1
> Of what I mean.
> b = 2
>
> And it can go on
> like this.
>
> c = 3
> For ever and ever.
>
> d = 4
>
> So how does one parse it?
>
> I want to split it into groups, such that if in yaml it would be:
>
> ---
> - |
> This is an example.
> a = 1
> - |
> Of what I mean.
> b = 2
>
> - |
> And it can go on
> like this.
>
> c = 3
> - |
> For ever and ever.
>
> d = 4
>
> - |
> So how does one parse it?
>
> I can write a line-by-line algorithm, but I figure there has to be an
> regexp that can do it more better.

This sorta works:

s = %{\
     This is an example.
         a = 1
     Of what I mean.
         b = 2

     And it can go on
     like this.

         c = 3
     For ever and ever.

         d = 4
}

a = s.scan(/(.*?(\s+)\s+[^\n]+?\n(?=\2\S|\z))/m)

a.each do |x|
   puts x
   puts "---"
end

__END__

Output:

     This is an example.
         a = 1

---
     Of what I mean.
         b = 2


---

     And it can go on
     like this.

         c = 3

---
     For ever and ever.

         d = 4



---