我有一个字符串(多行),我需要得到两个给定字符串之间的字符串。
如:
multiline_string = %q{testtestbegin
test1
test2
test3
end
test
xxx
xxx
begin
yyy
yyy
end
hhh
}
我需要找到“开始”和“结束”之间的字符串。有两个火柴我要把这两张打印出来。
有没有办法在regex中像
(?<=begin)(.*)(?=end)
请帮忙。
r = /
(?<=begin\n) # match 'begin' followed by a newline in a positive lookbehind
.*? # match any number of any character, lazily
(?=end\n) # match 'end' follwed by a newline in a positive lookahead
/mx # multiline and free-spacing regex definition modes
arr = multiline_string.scan(r)
#=> ["test1\ntest2\ntest3\n", "yyy\nyyy\n"]
arr.each { |s| puts s }
test1
test2
test3
yyy
yyy
我是一名优秀的程序员,十分优秀!