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

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.