From: kapheine@... (Zachary P. Landau) Date: 2003-07-24T04:47:32+09:00 Subject: Re: RegExp outermost () > s = 'a group ( stuff ( other )) (( hey ( you ) (there) ) stuff )' > > magic_re = # ? > s.scan(magic_re) { |match| puts match } > > output: > ( stuff ( other )) > (( hey ( you ) (there) ) stuff ) I think the easiest way to do this is with a stack. You push when you see a ( and pop when you see a ). The first entry on the stack is the start of a parens. Keep pushing and popping (kinky) until the stack is empty again. That would be the end of one group. Rinse and repeat.