Written by Sean Behan on Thu Aug 22nd 2013

This snippet will make a request to this page and extract the title from the title tag.

require 'open-uri'

html = open('http://www.seanbehan.com/how-to-extract-the-title-from-an-html-page-with-ruby').read

title = html.match(/(.*)<\/title>/) { $1 }</p> <p>puts title</p> </pre> <p>The regular expression here matches everything between the title tags. Anything within the parens "(.*)" is kept around as variable which you can access in the block with variables $1, $2, $3... $n, depending on how many matches are found.</p> </div> <br> <style media="screen"> .tags a { display:inline-block; margin:4px 1px; } </style> <div class="tags"> <i class="fa fa-tags"></i> Tagged with..<br> <a href="/tags/ruby/">#Ruby</a> <a href="/tags/regular+expressions/">#Regular Expressions</a> </div> <div id="comments"> <center> <!-- <img src="https://feenstrarhineline.com/assets/images/loading-8ea9594ca6.gif"/> --> <!-- <img src="https://cdn.dribbble.com/users/144388/screenshots/1081300/boatin-animated.gif" style="width:300px"/> --> <!-- <img src="http://www.mercurydockline.com/wp-content/themes/SevenForth/images/mercury-loader.gif"/> --> <img src="/static/coffee.gif" style="width:200px;"/> <p style="padding:0px;position:relative;top:-50px;"> <i>Just finishing up brewing up some fresh ground comments...</i> </p> </center> </div> <script type="text/javascript"> $(document).ready(function(){ $('#comments').load('/blog/comments.php?slug=how-to-extract-the-title-from-an-html-page-with-ruby'); $('body').on('submit', '#reply', function(e){ e.preventDefault(); $this = $(this) $data = $this.serialize() $('button', $this).attr('disabled', true).text('Submitting...') $.ajax({ method: 'POST', url: '/blog/comments.php?slug=how-to-extract-the-title-from-an-html-page-with-ruby', data: $data, success: function(resp){ if(resp.length>0){ $(resp).insertBefore($('#reply')) $('.clear', $this).val('') } $('button', $this).prop('disabled', false).text('Reply') } }); return false; }) }) </script> </div> <style media="screen"> .footer { margin:50px auto; max-width:960px; color:gray; font-size:14px; text-align:center; border-top:solid 1px #EEE; padding:30px 0px; } </style> <div class="footer"> © 2018 SeanBehan.com | <a href="/">Home</a>   <a href="https://xpath.app" target="_blank">XPath.app</a>   <a href="https://my.sharing.tools" target="_blank">Sharing.Tools</a> </div> <script>hljs.initHighlightingOnLoad();</script> <script type="text/javascript"> var _gauges = _gauges || []; (function() { var t = document.createElement('script'); t.type = 'text/javascript'; t.async = true; t.id = 'gauges-tracker'; t.setAttribute('data-site-id', '50b52f08f5a1f53f0e000032'); t.setAttribute('data-track-path', 'https://track.gaug.es/track.gif'); t.src = 'https://d36ee2fcip1434.cloudfront.net/track.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(t, s); })(); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-32797327-1', 'auto'); ga('send', 'pageview'); </script> </body> </html>