victorkhugo
victorkhugo

Reputation: 131

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.*?(?=picture))

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

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

Upvotes: 1

Views: 100

Answers (2)

victorkhugo
victorkhugo

Reputation: 131

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.

Upvotes: 0

Phrogz
Phrogz

Reputation: 303254

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]

Upvotes: 3

Related Questions