Ruby 拆分由子字符串列表分隔的字符串
我有如下数据:Ruby 拆分由子字符串列表分隔的字符串,ruby,string,Ruby,String,我有如下数据: str = "CODEA text for first item CODEB text for next item CODEB2 some"\ "more text CODEC yet more text" 和一份清单: arr = ["CODEA", "CODEB", "CODEB2", "CODEC", ... ] 我想把这个字符串分成一个散列。散列的键将是CODEA,CODEB,等等。散列的值将是后面的文本,直到下一个代码。输出应如下所示: "CODEA" =>
str = "CODEA text for first item CODEB text for next item CODEB2 some"\
"more text CODEC yet more text"
和一份清单:
arr = ["CODEA", "CODEB", "CODEB2", "CODEC", ... ]
我想把这个字符串分成一个散列。散列的键将是CODEA
,CODEB
,等等。散列的值将是后面的文本,直到下一个代码。输出应如下所示:
"CODEA" => "text for first item",
"CODEB" => "text for next item",
"CODEB2" => "some more text",
"CODEC" => "yet more text"
我们得到了一个刺和一个阵法
str = "CODEA text for first item CODEB text for next item " +
"CODEB2 some more text CODEC yet more text"
arr= %w|CODEC CODEB2 CODEA CODEB|
#=> ["CODEC", "CODEB2", "CODEA", "CODEB"]
b.to_a
#=> [["CODEA", "text", "for", "first", "item"],
# ["CODEB", "text", "for", "next", "item"],
# ["CODEB2", "some", "more", "text"],
# ["CODEC", "yet", "more", "text"]]
这是获得所需哈希的一种方法
str.split.
slice_before { |word| arr.include?(word) }.
map { |word, *rest| [word, rest.join(' ')] }.
to_h
#=> {"CODEA" =>"text for first item",
# "CODEB" =>"text for next item",
# "CODEB2"=>"some more text",
# "CODEC" =>"yet more text"}
看
步骤如下
a = str.split
#=> ["CODEA", "text", "for", "first", "item", "CODEB",
# "text", "for", "next", "item", "CODEB2", "some",
# "more", "text", "CODEC", "yet", "more", "text"]
b = a.slice_before { |word| arr.include?(word) }
#=> #<Enumerator:
# #<Enumerator::Generator:0x00005cbdec2b5eb0>:each>
last_word = ''
str.split.each_with_object({}) do |word,h|
if arr.include?(word)
h[word]=''
last_word = word
else
h[last_word] << ' ' unless h[last_word].empty?
h[last_word] << word
end
end
继续
c = b.map { |word, *rest| [word, rest.join(' ')] }
#=> [["CODEA", ["text for first item"]],
# ["CODEB", ["text for next item"]],
# ["CODEB2", ["some more text"]],
# ["CODEC", ["yet more text"]]]
c.to_h
#=> {"CODEA"=>"text for first item",
# "CODEB"=>"text for next item",
# "CODEB2"=>"some more text",
# "CODEC"=>"yet more text"}
下面可能是一个更好的方法
str.split.
slice_before { |word| arr.include?(word) }.
each_with_object({}) { |(word, *rest),h|
h[word] = rest.join(' ') }
当我还是个孩子的时候,可能会这样做
a = str.split
#=> ["CODEA", "text", "for", "first", "item", "CODEB",
# "text", "for", "next", "item", "CODEB2", "some",
# "more", "text", "CODEC", "yet", "more", "text"]
b = a.slice_before { |word| arr.include?(word) }
#=> #<Enumerator:
# #<Enumerator::Generator:0x00005cbdec2b5eb0>:each>
last_word = ''
str.split.each_with_object({}) do |word,h|
if arr.include?(word)
h[word]=''
last_word = word
else
h[last_word] << ' ' unless h[last_word].empty?
h[last_word] << word
end
end
last_word=''
str.split.each_with_object({})do|word,h|
如果包括?(word)
h[字]=“”
最后一个字
其他的
h[最后一个单词]我们被给予一个刺和一个数组
str = "CODEA text for first item CODEB text for next item " +
"CODEB2 some more text CODEC yet more text"
arr= %w|CODEC CODEB2 CODEA CODEB|
#=> ["CODEC", "CODEB2", "CODEA", "CODEB"]
b.to_a
#=> [["CODEA", "text", "for", "first", "item"],
# ["CODEB", "text", "for", "next", "item"],
# ["CODEB2", "some", "more", "text"],
# ["CODEC", "yet", "more", "text"]]
这是获得所需哈希的一种方法
str.split.
slice_before { |word| arr.include?(word) }.
map { |word, *rest| [word, rest.join(' ')] }.
to_h
#=> {"CODEA" =>"text for first item",
# "CODEB" =>"text for next item",
# "CODEB2"=>"some more text",
# "CODEC" =>"yet more text"}
看
步骤如下
a = str.split
#=> ["CODEA", "text", "for", "first", "item", "CODEB",
# "text", "for", "next", "item", "CODEB2", "some",
# "more", "text", "CODEC", "yet", "more", "text"]
b = a.slice_before { |word| arr.include?(word) }
#=> #<Enumerator:
# #<Enumerator::Generator:0x00005cbdec2b5eb0>:each>
last_word = ''
str.split.each_with_object({}) do |word,h|
if arr.include?(word)
h[word]=''
last_word = word
else
h[last_word] << ' ' unless h[last_word].empty?
h[last_word] << word
end
end
继续
c = b.map { |word, *rest| [word, rest.join(' ')] }
#=> [["CODEA", ["text for first item"]],
# ["CODEB", ["text for next item"]],
# ["CODEB2", ["some more text"]],
# ["CODEC", ["yet more text"]]]
c.to_h
#=> {"CODEA"=>"text for first item",
# "CODEB"=>"text for next item",
# "CODEB2"=>"some more text",
# "CODEC"=>"yet more text"}
下面可能是一个更好的方法
str.split.
slice_before { |word| arr.include?(word) }.
each_with_object({}) { |(word, *rest),h|
h[word] = rest.join(' ') }
当我还是个孩子的时候,可能会这样做
a = str.split
#=> ["CODEA", "text", "for", "first", "item", "CODEB",
# "text", "for", "next", "item", "CODEB2", "some",
# "more", "text", "CODEC", "yet", "more", "text"]
b = a.slice_before { |word| arr.include?(word) }
#=> #<Enumerator:
# #<Enumerator::Generator:0x00005cbdec2b5eb0>:each>
last_word = ''
str.split.each_with_object({}) do |word,h|
if arr.include?(word)
h[word]=''
last_word = word
else
h[last_word] << ' ' unless h[last_word].empty?
h[last_word] << word
end
end
last_word=''
str.split.each_with_object({})do|word,h|
如果包括?(word)
h[字]=“”
最后一个字
其他的
h[最后一个字]另一个选项
string.split.reverse
.slice_when { |word| word.start_with? 'CODE' }
.map{ |(*v, k)| [k, v.reverse.join(' ')] }.to_h
,在本例中返回此数组:
[["text", "more", "yet", "CODEC"], ["text", "more", "some", "CODEB2"], ["item", "next", "for", "text", "CODEB"], ["item", "first", "for", "text", "CODEA"]]
然后映射数组以构建所需的散列以获得结果(我没有反转散列):
另一种选择
string.split.reverse
.slice_when { |word| word.start_with? 'CODE' }
.map{ |(*v, k)| [k, v.reverse.join(' ')] }.to_h
,在本例中返回此数组:
[["text", "more", "yet", "CODEC"], ["text", "more", "some", "CODEB2"], ["item", "next", "for", "text", "CODEB"], ["item", "first", "for", "text", "CODEA"]]
然后映射数组以构建所需的散列以获得结果(我没有反转散列):
代码:
结果:
{"CODEA"=>"text for first item", "CODEB"=>"text for next item", "CODEB2"=>"some more text", "CODEC"=>"yet more text"}
代码:
结果:
{"CODEA"=>"text for first item", "CODEB"=>"text for next item", "CODEB2"=>"some more text", "CODEC"=>"yet more text"}
在String#split
中的模式中添加括号可以同时获得分隔符和字段
str.split(/(#{Regexp.union(*arr)})/).drop(1).each_slice(2).to_h
# =>
# {
# "CODEA"=>" text for first item ",
# "CODEB"=>"2 somemore text ",
# "CODEC"=>" yet more text"
# }
在String#split
中的模式中添加括号可以同时获得分隔符和字段
str.split(/(#{Regexp.union(*arr)})/).drop(1).each_slice(2).to_h
# =>
# {
# "CODEA"=>" text for first item ",
# "CODEB"=>"2 somemore text ",
# "CODEC"=>" yet more text"
# }
_这是什么意思?在我看来,您可以使用String#scan
方法。但请同时展示您尝试过的内容我的意思是,在数据分析开始之前,将在最终哈希中显示为键的子字符串[“CODEA”、“CODEB”、“CODEB2”、“CODEC”、…]
。它们不是从数据中提取的。我意识到我可以用一个正则表达式拆分这个示例字符串,该正则表达式查找以“CODE”开头的子字符串。然而,我希望有一种技术能够将已知键的数组应用于文本,而不是模式匹配文本。u是提前知道的。u这意味着什么?在我看来,您可以使用String#scan
方法。但请同时展示您尝试过的内容我的意思是,在数据分析开始之前,将在最终哈希中显示为键的子字符串[“CODEA”、“CODEB”、“CODEB2”、“CODEC”、…]
。它们不是从数据中提取的。我意识到我可以用一个正则表达式拆分这个示例字符串,该正则表达式查找以“CODE”开头的子字符串。然而,我希望有一种技术可以将已知键数组应用到文本,而不是模式匹配文本。我总是忘记splat |(k,*v)!我以前不知道.slice\u,它对这个问题非常有用。非常感谢你的回答!这段代码中有一个足够小的错误。结果散列没有像“text for first item”
这样的值,而是[“text”、“for”、“first”、“item”]
,谢谢您的提醒。我修好了。我总是忘了splat(k,*v)有多有用!我以前不知道.slice\u,它对这个问题非常有用。非常感谢你的回答!这段代码中有一个足够小的错误。结果散列没有像“text for first item”
这样的值,而是[“text”、“for”、“first”、“item”]
,谢谢您的提醒。我修好了。