From: Caleb Clausen Date: 2009-11-13T09:34:20+09:00 Subject: Re: DRYing a Regex On 11/12/09, RichardOnRails wrote: > I've got a routine that works fine at building an array of upper-case > strings extracted from a string: > > aNewList = [] > s = StringScanner.new sNewList > upper = /[A-Z]+/ > not_upper= Regexp.new( upper.source.sub( /\[/, '[^' ) ) > while not s.eos? > case > when s.skip(upper); aNewList << s.matched > else s.skip(not_upper) > end > end OTOH, you can rewrite it like this, and not have to even mention the complement of the match you're interested in: aNewList = [] s = StringScanner.new sNewList upper = /[A-Z]+/ aNewList<< s.matched while s.skip_until(upper) (Not tested real thoroughly, corner cases may break.)