On 11.12.2006 10:37, Carlos wrote: > Peter Szinek wrote: > >> Hello, >> >> I need to match a chunk of code like this: >> >> .... >> .... >> #begin here >> ...} >> ......end >> ...} >> ......} >> .....end >> ... >> ... >> >> I need to match from "the #begin here" up to the n-th closing token >> (i.e. '}' or 'end'). n can be arbitrary (let's consider that it is >> meaningful, i.e. there are no more '}' + 'end's than n. > > n = 4 > text =~ /#begin(.*(\}|end)){#{n}}/m > > ? > > (not tested). 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. robert