From: Carlos Date: 2006-12-11T19:02:31+09:00 Subject: Re: Regexp/scan question Peter Szinek wrote: > 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... To make it work with scan just make the parens non-capturing: irb(main):001:0> text = "#begin aaa end bbb } ccc } #begin ddd end eee end fff" => "#begin aaa end bbb } ccc } #begin ddd end eee end fff" irb(main):002:0> text.scan(/#begin(?:.*?(?:\}|end)){2}/m) => ["#begin aaa end bbb }", "#begin ddd end eee end"] Good luck. --