From: Peter Szinek Date: 2006-12-11T18:49:12+09:00 Subject: Re: Regexp/scan question 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 Sorry, I need to 'scan' it. I have been playing around with similar regexps, but they did not work out. E.g. also yours: 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"]] does not work with scan... Cheers, Peter __ http://www.rubyrailways.com