11.2. Writing regular expressions

Regular expressions are written with familiar characters—of course—but you have to learn to read and write them as things unto themselves. They’re not strings, and their meaning isn’t always as obvious as that of strings. They’re representations of patterns.

11.2.1. Seeing patterns

A regular expression (regexp or regex) specifies a pattern. For every such pattern, every string in the world either matches the pattern or doesn’t match it. The Ruby methods that use regular expressions use them either to determine whether a given string matches a given pattern or to make that determination and also take some action based on the answer.

Patterns of the kind specified by regular expressions are most easily understood, ...

Get The Well-Grounded Rubyist, Second Edition 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.