From: 7stud -- Date: 2007-11-23T19:34:56+09:00 Subject: Re: New at regexp and Ruby need help on parsing a string. Gabra Kadabra wrote: > I'm building a little test console for a ruby project. When using a > function I might get something like this: > > input_string ="and stuff and nice things not bad girls not greasy boys > and girlsandboys" > > As you already have guessed, I want the following in some kind of > format: > > smoking_table = {"and" => ["stuff", "nice things", "girlsandboys"], > "not" => ["bad girls","greasy boys"]} > > Thus, a regexp that splits a string on code words like "and" and "not" > is what I need. > > Please help me Try this: str = 'and stuff and nice things not bad girls not greasy boys and girlsandboys' smoking_table = {'and'=>[], 'not'=>[]} pieces = str.split(/(and |not )/) len = pieces.length index = 0 while index < len case pieces[index] when 'and ' smoking_table['and'] << pieces[index+1].strip index +=2 when 'not ' smoking_table['not'] << pieces[index+1].strip index += 2 else index += 1 end end p smoking_table -- Posted via http://www.ruby-forum.com/.