From: "Eric I." Date: 2008-07-19T12:38:43+09:00 Subject: Re: Suggestions for a parsing strategy? On Fri, Jul 18, 2008 at 10:59 PM, Robb wrote: > Hi all, > > I have input strings that can look like this: > > Common, Commerc(e, ial) > > I need to parse these into the three words that this represents: > > Common, Commerce, Commercial. This code does a lot of what you describe, providing the parenthetical only appears at the end. ==== s = "Common, Commerc(e, ial), Computer, Con(ic, ehead, temporary)" def parse_word_list(s) s.scan(/(\w+)(\((.*?)\))?/).map { |root, junk, suffixes| [root, suffixes && suffixes.split(", ")] } end list = parse_word_list(s) # see what's produced p list # use it to generate all words list.each do |root, suffix_list| if suffix_list suffix_list.each do |suffix| puts "#{root}#{suffix}" end else puts root end end ==== Hope that helps, Eric ==== LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE workshops. Please visit http://LearnRuby.com for all the details.