From: MenTaLguY Date: 2007-12-15T03:13:36+09:00 Subject: Re: simple way to turn "foo and bar" to "+foo +bar" On Sat, 15 Dec 2007 02:48:46 +0900, Max Williams wrote: > I want to add a slightly hacky feature into my boolean mysql search > which lets users write 'foo and bar', which is then translated into > '+foo +bar' (ie both 'foo' and 'bar' must be present) before being > passed to the search. Similarly, "foo and bar and snafu" should be > translated into "+foo +bar +snafu". > > I'm sure there must be a simple way to do this but i'm new to ruby and > have got bogged down in loads of nested ifs and exceptions already. > > Can anyone help with an elegant solution? query = "foo and bar and baz" nil while query.sub!(/\+*(\w+)\s+and\s+(\w+)/, '+\1 +\2') puts query -mental