开发者

Regex : Evaluating part of the expression

Let's say

sometext= "text text picture 1 picture 2 text text"

I want to write an expression that grabs the block of text before the pictures, and the first two pictures. Sometimes there will be only one picture but could be as many as five.

My first attempt was

parsed = sometext.scan(/picture.*?(picture.*?(?=pic开发者_开发知识库ture))

But it appears that Ruby does not support Regex if then statements.

[I edited this question to make it more clear.]


Here are various solutions, all of which give the same results:

ids = sometext.scan(/picture (\d+)/).flatten.map(&:to_i)
ids = sometext.scan(/(picture (\d+))/).map{ |str,id| id.to_i }
ids = sometext.scan(/(picture (\d+))/).map(&:last).map(&:to_i)
p ids
#=> [1, 2]


Here's what I wound up doing. I grabbed the whole chunk in one line of code and stuck it in an array.

var = sometext.scan(/goodtext.*?endofsection/m)

and then in another line made an array out of the chunks. I know the heading is first and an undetermined number of pictures follow, so I then limit the array down to three items.

var = var.collect{|x| x.scan(/heading|image).slice(0..2)}

I need to refine this more by gsub out the stuff other stuff I don't need, but I think this will satisfy my criteria. If anyone else can think of a more elegant way of doing this, I'm all for it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜