I have a string:
course_info =
"<course><date>10.10.09<date><title>Maths</title></course>"
I want to use a reg ex to extract the title from the string, in this
case "Maths"
So far I came up with:
course_info.gsub!(/^(.*)<title>/, "")
course_info.gsub!(/<\/title>(.*)$/, "").chomp!
p course_info
# -> "Maths"
Is it possible amalgamate these two regular expressions into one or to
improve this in any way?
Am grateful for any help.
···
--
Posted via http://www.ruby-forum.com/.
Slim2:~ phrogz$ irb
irb(main):001:0> course_info =
"<course><date>10.10.09<date><title>Maths</title></course>"
=> "<course><date>10.10.09<date><title>Maths</title></course>"
irb(main):002:0> course_info[ /<title>([^<]+)/, 1 ]
=> "Maths"
irb(main):003:0> %r{<title>(.+?)</title>}.match(course_info).to_a
=> ["<title>Maths</title>", "Maths"]
···
On Oct 9, 6:46 am, Jim Burgess <jack.ze...@gmail.com> wrote:
course_info =
"<course><date>10.10.09<date><title>Maths</title></course>"
I want to use a reg ex to extract the title from the string, in this
case "Maths"
This looks like an XML string. Did course_info come from an XML
document? An XML parser like Hpricot or Nokogiri would be an
improvement.
···
On Oct 9, 8:46 am, Jim Burgess <jack.ze...@gmail.com> wrote:
I have a string:
course_info =
"<course><date>10.10.09<date><title>Maths</title></course>"
I want to use a reg ex to extract the title from the string, in this
case "Maths"
So far I came up with:
course_info.gsub!(/^(.*)<title>/, "")
course_info.gsub!(/<\/title>(.*)$/, "").chomp!
p course_info
# -> "Maths"
Is it possible amalgamate these two regular expressions into one or to
improve this in any way?
Hi! I suggest the following:
text="<course><date>10.10.09<date><title>Maths</title></course>"
text.scan(/<title>([^<]+)</title>/) do
puts "#{$1}"
end
···
On Fri, Oct 9, 2009 at 4:46 PM, Jim Burgess <jack.zelig@gmail.com> wrote:
I have a string:
course_info =
"<course><date>10.10.09<date><title>Maths</title></course>"
I want to use a reg ex to extract the title from the string, in this
case "Maths"
So far I came up with:
course_info.gsub!(/^(.*)<title>/, "")
course_info.gsub!(/<\/title>(.*)$/, "").chomp!
p course_info
# -> "Maths"
Is it possible amalgamate these two regular expressions into one or to
improve this in any way?
Am grateful for any help.
--
Posted via http://www.ruby-forum.com/\.
Thanks very much for that.
It'll take me a while to figure out what you've done, but that seems to
work perfectly!
Gavin Kistner wrote:
···
Slim2:~ phrogz$ irb
irb(main):001:0> course_info =
"<course><date>10.10.09<date><title>Maths</title></course>"
=> "<course><date>10.10.09<date><title>Maths</title></course>"
irb(main):002:0> course_info[ /<title>([^<]+)/, 1 ]
=> "Maths"
irb(main):003:0> %r{<title>(.+?)</title>}.match(course_info).to_a
=> ["<title>Maths</title>", "Maths"]
--
Posted via http://www.ruby-forum.com/\.
This looks like an XML string. Did course_info come from an XML
document? An XML parser like Hpricot or Nokogiri would be an
improvement.
That's a good idea. Thanks, I'll look into that.
···
--
Posted via http://www.ruby-forum.com/\.
This looks like an XML string. Did course_info come from an XML
document? An XML parser like Hpricot or Nokogiri would be an
improvement.
Quick update:
Just installed and followed a quick tutorial on Hpricot.
While effectively accomplishing the same task, this parser makes the
code much easier to read. I can now write:
doc = Hpricot.parse(File.read("courses.xml"))
(doc/:course).each do |course|
if (course/:date).inner_html.match "#{date}"
groups_that_had_lessons_this_month << (course/:title).inner_html
end
end
as opposed to this (or worse):
File.open("courses.txt", 'r') do |datei|
datei.readlines.select do |line|
if line.match "#{date}"
groups_that_had_lessons_this_month << line[ /<title>([^<]+)/, 1 ]
end
end
end
Thanks for the recommendation, and thanks everyone else for the answers
too.
···
--
Posted via http://www.ruby-forum.com/\.