所以像这样的东西?我假设您的分隔符在整个字符串:
中,
是一致的,并且使用双引号"
将所需的字符串括起来。
# escape double quotes
longstring = %q(bunchofotherstringstuffandcharacters"hisquote":"The most important aspect of the painting was the treatment of lighting.","lp":andthenalotmorestringandcharacters)
# split on double quotes
substrings = longstring.split("\"").to_enum
# somewhere to sure the strings you want
save = []
# use a rescue clause to detect that the enumerator 'substrings' as reached an end
begin
while true do
remember = substrings.next
case substrings.peek # lets see if that next element is our deliminator
when ":" # Once the semicolon is spotted ahead, grab the three strings we want.
save << remember
substrings.next # skip the ":"
save << substrings.next
substrings.next # skip the ","
save << substrings.next
end
end
rescue StopIteration => e
puts "End of Substring Enumeration was reached."
ensure
puts save.inspect #=> ["hisquote", "The most important aspect of the painting was the treatment of lighting.", "lp"]
end