Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Outputting <title></title> content from HTML

Original Question: "Writing a program that reads from a public web page and looks for the title of the page. The program should then output the contents of the page title to STDOUT using an ERB template."

My problem: I've been able to retrieve the HTML of a webpage and save it to a txt file; however, i'm a bit confused as to how I can get the program to output the content of the <title> element. I know that I can find the <title></title> tags using .include?(), /title/, etc., but when it comes to actually outputting the content, i'm a bit lost.

Without telling me the answer, would someone mind maybe pointing me in the right direction or giving me a hint?

In other words, if the title is

            <title>My Name</title>

How can I get ruby to search the .txt file and spit out "My Name"?

Thank you!

2 Answers

I don't know Ruby myself, but my best guess would be to use regular expressions. Something like <[^>]+> allows you to find opening and closing tags which you can then exclude.


This is probably better for your purpose:


That should match anything in between two title tags.

[edit 2] This also works in case there are different tags inside the tag you're searching:


Thank you, Niklas. I guess I just didn't know how to write the right regexp.