Making one reg ex out of two

J

Jim Burgess

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.
 
P

Phrogz

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"

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"]
 
J

Jim Burgess

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 said:
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"]
 
B

Bertram Scharpf

Hi,

Am Freitag, 09. Okt 2009, 22:27:01 +0900 schrieb Jim Burgess:
Gavin said:
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"]

Thanks very much for that.
It'll take me a while to figure out what you've done, but that seems to
work perfectly!

Shorter:

course_info =~ %r{<title>(.+?)</title>}
$1 == "Maths"

Bertram
 
M

Mark Thomas

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?

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.
 
J

Jim Burgess

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.
 
A

Alexey Bovanenko

Hi! I suggest the following:

text="<course><date>10.10.09<date><title>Maths</title></course>"

text.scan(/<title>([^<]+)</title>/) do
puts "#{$1}"
end
 
J

Jim Burgess

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.
 

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

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top