# Making one reg ex out of two

**URL:** <https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996>\
**Category:** Ruby\
**Created:** [October 9, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996 "2009-10-09T14:46:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 9, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/1 "2009-10-09T14:46:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 9, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/2 "2009-10-09T15:14:00Z")

</div>

On Oct 9, 6:46 am, Jim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”]

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 9, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/3 "2009-10-09T15:26:59Z")

</div>

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”]

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 9, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/4 "2009-10-09T17:05:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 13, 2009, 7:44am UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/5 "2009-10-13T07:44:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 13, 2009, 8:20am UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/6 "2009-10-13T08:20:54Z")

</div>

Hi! I suggest the following:

text=“10.10.09Maths”

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

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 12, 2009, 9:12pm UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/7 "2009-10-12T21:12:21Z")

</div>

On Oct 9, 8:46 am, Jim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [October 13, 2009, 9:39am UTC](https://www.ruby-forum.com/t/making-one-reg-ex-out-of-two/176996/8 "2009-10-13T09:39:39Z")

</div>

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