11.3. Building a pattern in a regular expression

When you write a regexp, you put the definition of your pattern between the forward slashes. Remember that what you’re putting there isn’t a string but a set of predictions and constraints that you want to look for in a string.

The possible components of a regexp include the following:

  • Literal characters, meaning “match this character”
  • The dot wildcard character (.), meaning “match any character” (except \n, the newline character)
  • Character classes, meaning “match one of these characters”

We’ll discuss each of these in turn. We’ll then use that knowledge to look more deeply at match operations.

11.3.1. Literal characters in patterns

Any literal character you put in a regexp matches itself in ...

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.