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

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

Steel S. 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.

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” [email protected] wrote in message
news:[email protected]

On 19.10.2010 19:51, Greg Ma wrote:

if you wish, remove Location: and obtain;

str2 = str1.gsub(/Location:\s+/i,’’)
puts “str2: #{str2}” #=> Columbus, Ohio

HTH gfb

"Greg Ma"[email protected] wrote in message
news:[email protected]

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

Here’s a bit more to play with:

http://gist.github.com/635002

Kind regards

robert

Robert K. wrote in post #955554:

On 19.10.2010 19:51, Greg Ma wrote:

if you wish, remove Location: and obtain;

str2 = str1.gsub(/Location:\s+/i,’’)
puts “str2: #{str2}” #=> Columbus, Ohio

HTH gfb

"Greg Ma"[email protected] wrote in message
news:[email protected]

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

Here’s a bit more to play with:

http://gist.github.com/635002

Kind regards

robert
require ‘cgi’ —> CGI.unescapeHTML is more to the point, but pretty
much what Robert posted. You’re dealing with an escaped fragment of
HTML; treat it as such.

Gianfranco Bozzetti wrote in post #955473:

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” [email protected] wrote in message
news:[email protected]

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs