# Making one reg ex out of two

**URL:** <https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732>\
**Category:** ruby-talk\
**Created:** [9 October 2009 12:46 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732 "2009-10-09T12:46:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Burgess1](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@Jim\_Burgess1](https://rubytalk.org/u/Jim_Burgess1)\
**Post date:** [9 October 2009 12:46 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/1 "2009-10-09T12:46:09Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [9 October 2009 13:10 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/2 "2009-10-09T13:10:06Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![Mark\_Thomas](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark\_Thomas](https://rubytalk.org/u/Mark_Thomas)\
**Post date:** [12 October 2009 19:10 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/3 "2009-10-12T19:10:08Z")

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

> **···**
>
> 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?

---

<div class="post-metadata">

**Author:** ![Alexey\_Bovanenko](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Alexey\_Bovanenko](https://rubytalk.org/u/Alexey_Bovanenko)\
**Post date:** [13 October 2009 06:20 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/4 "2009-10-13T06:20:18Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jim\_Burgess1](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@Jim\_Burgess1](https://rubytalk.org/u/Jim_Burgess1)\
**Post date:** [9 October 2009 13:27 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/5 "2009-10-09T13:27:01Z")

</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 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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jim\_Burgess1](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@Jim\_Burgess1](https://rubytalk.org/u/Jim_Burgess1)\
**Post date:** [13 October 2009 05:44 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/6 "2009-10-13T05:44:03Z")

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jim\_Burgess1](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@Jim\_Burgess1](https://rubytalk.org/u/Jim_Burgess1)\
**Post date:** [13 October 2009 07:39 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/7 "2009-10-13T07:39:41Z")

</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|  
&nbsp;&nbsp;if (course/:date).inner\_html.match "#{date}"  
&nbsp;&nbsp;&nbsp;&nbsp;groups\_that\_had\_lessons\_this\_month \<\< (course/:title).inner\_html  
&nbsp;&nbsp;end  
end

as opposed to this (or worse):

File.open("courses.txt", 'r') do |datei|  
&nbsp;&nbsp;datei.readlines.select do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;if line.match "#{date}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups\_that\_had\_lessons\_this\_month \<\< line[/\<title\>([^\<]+)/, 1 ]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [9 October 2009 15:04 UTC](https://rubytalk.org/t/making-one-reg-ex-out-of-two/55732/8 "2009-10-09T15:04:53Z")

</div>

Hi,

> **···**
>
> Am Freitag, 09. Okt 2009, 22:27:01 +0900 schrieb Jim Burgess:
> 
> > 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"]
> > 
> > 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:
> 
> &nbsp;&nbsp;course\_info =~ %r{\<title\>(.+?)\</title\>}  
> &nbsp;&nbsp;$1 == "Maths"
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)
