Wrapping Up

We started this chapter by using the match method both to check whether a string matched a particular pattern and to extract capture groups from within that pattern. Then we explored the scan method on strings, and how we could use it to extract many different patterns from within a string.

As well as simply matching patterns, we discovered how to use sub and gsub to take portions of text that match a pattern and replace them with something else. We also covered gsub’s ability to take a block, and we used that to perform some further processing of the matched text before replacing it.

Next up, we’ll be looking at an alternative technique to regular expressions—parsers—and when they can be useful.

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.