Jet Koten wrote:
> Hi all,
> 
> I'm new to Ruby and even newer to regex. I'm trying to write my first
> [useful] Ruby program and need a way to cut out an unneeded prefix
> substring and retain the substring that comes after it.
> 
> Here are the actual details from my code:
> 
> result.each do |item|
>   price = item.search(".price").text.match(/\d+[.]\d+/)
>   condition = item.search(".condition").text.match(/Used - ([^,]+)/)
>   rating = item.search(".rating a").text.to_i
>   seller = item.search(".seller b").text
>   puts "#{price} - #{condition} - #{rating} - #{seller}"
> end
> 
> The one from condition [in the code above] is the one that is giving me
> a challenge. The string that is sent to condition will always be exactly
> one of the following and nothing else at all:
> 
> "Used - Like New"
> "Used - Very Good"
> "Used - Good"
> "Used - Acceptable"
> (...)

This is another option, avoiding regular expressions. It's kind of old 
school, but it's fast, flexible, and handles garbage.

sanitize_condition = Hash.new("Unknown")
sanitize_condition["Used - Like New"] = "Like New"
sanitize_condition["Used - Very Good"] = "Very Good"
sanitize_condition["Used - Good"] = "Good"
sanitize_condition["Used - Acceptable"] = "Acceptable"
sanitize_condition["Used - Broken"] = "Kaput"

demo_conditions = ["Used - Like New","",nil,"Used - Broken","Used - 
Acceptable","garble"]
demo_conditions.each{|cond| puts sanitize_condition[cond] }

hth,

Siep
-- 
Posted via http://www.ruby-forum.com/.