Hi max,
How about...
irb(main):001:0> "foo and bar".sub(/(foo) and (bar)/, "+#{$1} +#{$2}")
=> "+foo +bar"
irb(main):002:0> "foo and bar".sub(/(foo) and (bar)/, '+\1 +\2')
=> "+foo +bar"
irb(main):003:0> "foo and bar and snafu".sub(/(foo) and (bar) and
(snafu)/, '+\1 +\2 +\3')
=> "+foo +bar +snafu"
or
irb(main):004:0> "foo and bar and snafu".gsub(/(\w+)\s+and/,
'+\1').sub(/(\w+)$/, '+\1')
=> "+foo +bar +snafu"
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?
>
> thanks
> max