O'Reilly logo

Text Processing with Ruby by Rob Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example: Extracting URLs

In the previous example, we used read to read all the standard input in one go. But as we saw with files, reading everything into memory in one gulp often isn’t the best idea, especially when our input begins to grow in size.

It was also annoying in the previous example that we had to type ruby to-uppercase.rb. Other commands are short and snappy—cut, grep—but we had to type what feels like a lot of superfluous information.

For our next example, we’re going to write a script that extracts URLs from the input passed to it, outputting any that it finds and ignoring the rest of the input. So, if we passed it the following text:

 
Alice's website is at http://www.example.com
 
While Jane's website is at https://example.net ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required