在ruby中匹配模式后提取数据

时间:2017-06-20 10:45:47

标签: ruby

puts myvalue[1](我的值是一个字符串)

#My COmments
#Checked on 54
[Roll]
new line = see, that, cfiu\poli
check_that = roll\uiju, rolldeny that, its=oik, mao\ikir4
stares = roll\okik, roll\asde4, roll\kz98e

我正在尝试在Roll之后提取并分配到数组

groups = myvalue[1].match(/^[Roll].*/)

我得到的输出

new line = see that cfiu\poli

而不是(预期的)

new line = see that cfiu\poli
check_that = roll\uiju, rolldeny that, its=oik, mao\ikir4
stares = roll\okik, roll\asde4, roll\kz98e

怎么做,谢谢

2 个答案:

答案 0 :(得分:1)

更改您的论坛分配行:

groups = myvalue[1].partition(/\[Roll\]/).last

答案 1 :(得分:1)

> string = "String begins here
[Roll]
new line = this is new line content
check_that = check that line content
stares = stares content"

> required_string = string.split("[Roll]").last.strip
# new line = this is new line content
# check_that = check that line content
# stares = stares content

更新

> first_arry = required_string.split("\n").map{|e| e.split("=")}.map(&:first)
#=> ["new line ", "check_that ", "stares "] 
> second_arry = required_string.split("\n").map{|e| e.split("=")}.map(&:last)
#=> [" this is new line content", " check that line content", " stares content"]