I am having a string in Ruby. I need to iterate through the brackets in the string.
My String:
(((MILK AND SOYA) OR NUT) AND COCONUT)
First Iteration should return:
((MILK AND SOYA) OR NUT) AND COCONUT
Second Iteration should return the below:
(MILK AND SOYA) OR NUT
Third Iteration should return the following text:
MILK AND SOYA
How to do this in Ruby? Thanks in advance for the help.
thy this solution:
str = "(((MILK AND SOYA) OR NUT) AND COCONUT)"
while not str.nil?
puts str = str.match(/\((.*)\)/).to_a.last
end # =>
((MILK AND SOYA) OR NUT) AND COCONUT
(MILK AND SOYA) OR NUT
MILK AND SOYA
regex /\((.*)\)/
searches for string inside brackets
@DmitryCat's solution works fine with your example, but it seems you might be interested in the innermost brackets first.
So you'll need to make sure the characters between brackets aren't brackets :
str = "(((MILK AND SOYA) OR NUT) AND COCONUT)"
while str.gsub!(/\(([^\(\)]+)\)/){ p $1; ''}
end
# "MILK AND SOYA"
# " OR NUT"
# " AND COCONUT"
With "((MILK AND SOYA) OR (MILK AND NUT))"
it outputs :
# "MILK AND SOYA"
# "MILK AND NUT"
# " OR "
Regexen probably aren't the right tool for this job.
This parser
gem would have no problem analysing your expression :
require 'parser/current'
str = "(((MILK AND SOYA) OR NUT) AND COCONUT)"
p Parser::CurrentRuby.parse(str.gsub(/\b(and|or)\b/i){|s| s.downcase})
# s(:begin,
# s(:and,
# s(:begin,
# s(:or,
# s(:begin,
# s(:and,
# s(:const, nil, :MILK),
# s(:const, nil, :SOYA))),
# s(:const, nil, :NUT))),
# s(:const, nil, :COCONUT)))
You now have a tree : a Root node and children
method. Which you can call recursively to get any information about your expression.
(Thanks to @Casper for this suggestion)
It looks like sexp
gem might also work, possibly with an easier syntax than parser
:
require 'sxp'
p SXP.read("(((MILK AND SOYA) OR (NUT AND SOYA)) AND COCONUT)")
# [[[:MILK, :AND, :SOYA], :OR, [:NUT, :AND, :SOYA]], :AND, :COCONUT]
As mentioned by @Casper in the comments (thanks again!), you're trying to reinvent the wheel. If you need full text search for Rails with boolean expressions, Sphinx is a great tool. It's fast, good, reliable and there's an adapter for Ruby/Rails : thinkingsphinx.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With