From: Raul Parolari Date: 2007-11-23T19:47: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 # One possible implementation is: smoking_table = { :and => [], :not => [] } str.scan(/ (and|not) (.*?) (?= \band|\bnot|$) /x) do |k, v| smoking_table[k.to_sym].push(v.strip) end => {:and => ["stuff", "nice things", "girlsandboys"], :not => ["bad girls", "greasy boys"]} I hope that this works for you, Raul -- Posted via http://www.ruby-forum.com/.