Greg_Ma
(Greg Ma)
19 October 2010 12:27
1
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
路路路
--
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
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/\ .
Greg_Ma
(Greg Ma)
19 October 2010 14:05
3
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/\ .
Greg_Ma
(Greg Ma)
19 October 2010 17:51
5
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" <gregorylepacha@msn.com> wrote in message
news:14b91fa99bddaa2bd5b39c486fd8d1ce@ruby-forum.com...
Thanks this is what I was looking for
I really need to get learning regular expressions...
路路路
--
Posted via http://www.ruby-forum.com/\ .
Robert_K1
(Robert K.)
19 October 2010 20:15
6
Here's a bit more to play with:
html-extract.rb
#!/bin/env ruby19
require 'nokogiri'
# Nokogiri should have this as well
REPL = {
'<' => '<',
'≤' => '<=',
'>' => '>',
'≥' => '>=',
This file has been truncated. show original
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="<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...
Thanks this is what I was looking for
I really need to get learning regular expressions...
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/