Find block in a long string

G

Greg Ma

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 :)

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

Greg
 
S

Steel Steel

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 :)

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::Tutorial.en
 
G

Greg Ma

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

news:[email protected]...

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

Robert Klemme

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

news:[email protected]...

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

Here's a bit more to play with:

http://gist.github.com/635002

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top