Find block in a long string

Hi,

I am parsing a rss feed and i am try to extract a specific element from
an attribute.

The attribute looks like this, and I would like to extract the location
from it.
"<p>\n <strong>Location:</strong> Columbus,
Ohio\n</p>\n\n<"

Here is the horrible code I've done, could you help me to make it
cleaner :slight_smile:

聽聽聽聽聽聽description.split("\n").each do |ugly|
聽聽聽聽聽聽聽聽next unless ugly.include?("Location")
聽聽聽聽聽聽聽聽location = ugly.split("\n")[1].split("gt;").last
聽聽聽聽聽聽end

Greg

路路路

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

Greg Ma wrote in post #955398:

Hi,

I am parsing a rss feed and i am try to extract a specific element from
an attribute.

The attribute looks like this, and I would like to extract the location
from it.
"<p>\n <strong>Location:</strong> Columbus,
Ohio\n</p>\n\n<"

Here is the horrible code I've done, could you help me to make it
cleaner :slight_smile:

      description.split("\n").each do |ugly|
        next unless ugly.include?("Location")
        location = ugly.split("\n")[1].split("gt;").last
      end

Greg

Ruby has an RSS parser isn't it? Why not use it?

http://ruby-doc.org/core/classes/RSS.html
http://www.cozmixng.org/~rwiki/?cmd=view;name=RSS+Parser%3A%3ATutorial.en

路路路

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

Steel Steel wrote in post #955407:

Ruby has an RSS parser isn't it? Why not use it?

http://ruby-doc.org/core/classes/RSS.html
http://www.cozmixng.org/~rwiki/?cmd=view;name=RSS+Parser%3A%3ATutorial.en

I do use a rss parser. The rss parser was just here to explain what I am
doing, but it has no relation with my issue.

路路路

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

Does this snippet solve your need?

# Given the string:
str="<p>\n <strong>Location:</strong> Columbus,
Ohio\n</p>\n\n<"
# remove unwanted chars with:
str1=str.gsub(/[&\/;\n]+|lt|gt|strong|p/im,' ').strip.squeeze(' ')
# and obtain;
puts "str1: #{str1}" #=> Location: Columbus, Ohio
# if you wish, remove Location: and obtain;
str2 = str1.gsub(/Location:\s+/i,'')
puts "str2: #{str2}" #=> Columbus, Ohio

HTH gfb

"Greg Ma" <gregorylepacha@msn.com> wrote in message
news:14b91fa99bddaa2bd5b39c486fd8d1ce@ruby-forum.com...

路路路

Steel Steel wrote in post #955407:

Ruby has an RSS parser isn't it? Why not use it?

http://ruby-doc.org/core/classes/RSS.html
http://www.cozmixng.org/~rwiki/?cmd=view;name=RSS+Parser%3A%3ATutorial.en

I do use a rss parser. The rss parser was just here to explain what I am
doing, but it has no relation with my issue.

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

Gianfranco Bozzetti wrote in post #955473:

Does this snippet solve your need?

# Given the string:
str="&lt;p&gt;\n &lt;strong&gt;Location:&lt;/strong&gt; Columbus,
Ohio\n&lt;/p&gt;\n\n&lt;"
# remove unwanted chars with:
str1=str.gsub(/[&\/;\n]+|lt|gt|strong|p/im,' ').strip.squeeze(' ')
# and obtain;
puts "str1: #{str1}" #=> Location: Columbus, Ohio
# if you wish, remove Location: and obtain;
str2 = str1.gsub(/Location:\s+/i,'')
puts "str2: #{str2}" #=> Columbus, Ohio

HTH gfb

"Greg Ma" <gregorylepacha@msn.com> wrote in message
news:14b91fa99bddaa2bd5b39c486fd8d1ce@ruby-forum.com...

Thanks this is what I was looking for :slight_smile:
I really need to get learning regular expressions...

路路路

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

Here's a bit more to play with:

Kind regards

  robert

路路路

On 19.10.2010 19:51, Greg Ma wrote:

Gianfranco Bozzetti wrote in post #955473:

Does this snippet solve your need?

# Given the string:
str="&lt;p&gt;\n&lt;strong&gt;Location:&lt;/strong&gt; Columbus,
Ohio\n&lt;/p&gt;\n\n&lt;"
# remove unwanted chars with:
str1=str.gsub(/[&\/;\n]+|lt|gt|strong|p/im,' ').strip.squeeze(' ')
# and obtain;
puts "str1: #{str1}" #=> Location: Columbus, Ohio
# if you wish, remove Location: and obtain;
str2 = str1.gsub(/Location:\s+/i,'')
puts "str2: #{str2}" #=> Columbus, Ohio

HTH gfb

"Greg Ma"<gregorylepacha@msn.com> wrote in message
news:14b91fa99bddaa2bd5b39c486fd8d1ce@ruby-forum.com...

Thanks this is what I was looking for :slight_smile:
I really need to get learning regular expressions...

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/