Making one reg ex out of two

I have a string:

course_info =
“10.10.09Maths”

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!(/^(.)/, “”)
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.

On Oct 9, 6:46 am, Jim B. [email protected] wrote:

course_info =
“10.10.09Maths”

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 =
“10.10.09Maths”
=> “10.10.09Maths”

irb(main):002:0> course_info[ /([^<]+)/, 1 ]
=> “Maths”

irb(main):003:0> %r{(.+?)}.match(course_info).to_a
=> [“Maths”, “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!

Gavin K. wrote:

Slim2:~ phrogz$ irb
irb(main):001:0> course_info =
“10.10.09Maths”
=> “10.10.09Maths”

irb(main):002:0> course_info[ /([^<]+)/, 1 ]
=> “Maths”

irb(main):003:0> %r{(.+?)}.match(course_info).to_a
=> [“Maths”, “Maths”]

Hi,

Am Freitag, 09. Okt 2009, 22:27:01 +0900 schrieb Jim B.:

=> [“Maths”, “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{(.+?)}
$1 == “Maths”

Bertram

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.

Hi! I suggest the following:

text=“10.10.09Maths”

text.scan(/([^<]+)/) do
puts “#{$1}”
end

On Oct 9, 8:46 am, Jim B. [email protected] wrote:

course_info.gsub!(/^(.)/, “”)
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.

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[ /([^<]+)/, 1 ]
end
end
end

Thanks for the recommendation, and thanks everyone else for the answers
too.

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