这不是一个漂亮的正则表达式,但如果我正确理解您的输入和要求......呃“some_data”应该被忽略吧?
strin = "some_data I POST postdata_1 IV POST postdata_4 III POST postdata_3 II POST postdata_2"
while curmatch = strin.match(/ (.*?) POST (.*?) POST/) do
postnum = curmatch[1]
postdata = curmatch[2].reverse.sub(/.*? /, '').reverse
puts "<post number>#{postnum}</post number>"
puts "<post data>#{postdata}</post data>"
strin.sub!(" #{postnum} POST #{postdata}", '')
end
curmatch = strin.match(/ (.*?) POST (.*)/)
puts "<post number>#{curmatch[1]}</post number>"
puts "<post data>#{curmatch[2]}</post data>"
输出:
<post number>I</post number>
<post data>postdata_1</post data>
<post number>IV</post number>
<post data>postdata_4</post data>
<post number>III</post number>
<post data>postdata_3</post data>
<post number>II</post number>
<post data>postdata_2</post data>