> IMHO this does not work because of the greedy ".*". You could try with > reluctant, i.e. ".*?". Also the grouping does not catch the whole > sequence. Yeah, I tried to correct these problems but I am still not quite there... Carlos' regexp, vol 2 (with greedy ?) irb(main):007:0> text = '.... #begin aaaa end bbb } ccc end ddd' => ".... #begin aaaa end bbb } ccc end ddd" irb(main):008:0> n = 2 => 2 irb(main):009:0> text.scan(/#begin(.*?(\}|end)){#{n}}/m) => [[" ccc end", "end"]] And I would like to get [["#begin aaaa end bbb }"]] OK, I know that I did not specify the problem correctly for the first time, maybe now it is more clear... Cheers, Peter __ http://www.rubyrailways.com