Morton Goldberg
12/7/2007 5:56:00 PM
On Dec 7, 2007, at 6:22 AM, dare ruby wrote:
> if @news contains 'kick the ball"
> i need out put as, like
>
> kick
> the
> ball
> same if @ news contains 'kick the="size" ball="blue""
>
> i need to split and output should be like
>
> kick
> the
> ball
> Some message : size
> blue
I'm not sure I understand your question, but here is a guess on how
you might get the output you want.
<code>
str1 = "kick the ball"
str2 = 'kick the="size" ball="blue"'
def my_split(str, msg)
str = str.gsub(/=\"/, ' #').split(/(?:\"\s*)|\s/)
words, more = str.partition { |token| token[0] != ?# }
unless more.empty?
words << msg
words << more.map { |token| token.delete("#") }
end
words
end
puts my_split(str1, "MESSAGE 1")
puts
puts my_split(str2, "MESSAGE 2")
</code>
<results>
kick
the
ball
kick
the
ball
MESSAGE 2
size
blue
</results>
It's not very elegant, but maybe it will work for you.
Regards, Morton