我正在使用Ruby 2.2来解析以下文本:
[key1: this is a bunch of text that can
span multiple lines.
key2: foo
key2: bar
key3: this can span multiple lines
as well
]
进入一个看起来像这样的哈希数组:
[
key1: "this is a bunch of text that can span multiple lines."
key2: ["foo", "bar"]
key3: "this can span multiple lines as well"
]
我的第一个目标是提出正则表达式来解析键/值对,这就是我所坚持的:
/\[((key1|key2|key3): (.+?))+(?=(?:key1:|key2:|key3:|\]))/m
它不起作用,因为我用来寻找下一个键或右括号的前瞻似乎是匹配文本。我的理解是它没有。
任何建议都将不胜感激。感谢。
答案 0 :(得分:1)
data = %Q|[key1: this is a bunch of text that can
span multiple lines.
key2: foo
key2: bar
key3: this can span multiple lines
as well
]|
p data[1..-2] #Remove square brackets [...]
.split(/(key\d):\s+/)[1..-1] #regexp out keys and values. (And get rid of initial empty string)
.each_slice(2) #Group into key-value lists
.group_by(&:shift) # Group by first values