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

Simple Parsers with StringScanner

The essence of StringScanner is in its name. It lets you scan through a string, moving through it as you capture information, always knowing where in the string you are. Unlike with a regular expression, at any point in our scanning through the string we can stop and make decisions. We can look ahead to see what’s coming next, and take a different course of action depending on the answer. We can use recursion, too, to match nested constructs.

StringScanner manages this scanning process by maintaining a scan pointer. This behaves in effectively the same way as a cursor in a text editor does, indicating what part of the string we’ve moved past and what part is yet to come. We can then use varying methods to advance ...

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